之前做一个模仿Amg的音乐网站,因为先期对业务的复杂性考虑不够周全,没有编写接口,结果维护变得很狼狈,所以在这里做了一个mini 泛型接口:
using System;
using System.Collections.Generic;
using System.Text;
namespace TestUnit
{
/// <summary>
/// 管理员类接口
/// 泛型T代指专辑类
/// </summary>
interface IAdmin<T> where T:IComparable
{
/// <summary>
/// 返回当前月所有的天数
/// </summary>
IList<T> getAllAlbumList();
}
}
using System.Collections.Generic;
using System.Text;
namespace TestUnit
{
/// <summary>
/// 管理员类接口
/// 泛型T代指专辑类
/// </summary>
interface IAdmin<T> where T:IComparable
{
/// <summary>
/// 返回当前月所有的天数
/// </summary>
IList<T> getAllAlbumList();
}
}
然后实现它:
using System;
using System.Collections.Generic;
using System.Text;
namespace TestUnit
{
/// <summary>
/// 实现IADMIN接口
/// </summary>
public class Admin:IAdmin<string>
{
public IList<string> getAllAlbumList()
{
IList<string> list = new List<string>();
int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
list.Add(string.Format(
"{0,-4}年{1,-2}月有{2,-2}天", DateTime.Now.Year, DateTime.Now.Month,days
));
for (int i = 1; i <= days; i++)
{
list.Add("day "+i.ToString());
}
return list;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace TestUnit
{
/// <summary>
/// 实现IADMIN接口
/// </summary>
public class Admin:IAdmin<string>
{
public IList<string> getAllAlbumList()
{
IList<string> list = new List<string>();
int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
list.Add(string.Format(
"{0,-4}年{1,-2}月有{2,-2}天", DateTime.Now.Year, DateTime.Now.Month,days
));
for (int i = 1; i <= days; i++)
{
list.Add("day "+i.ToString());
}
return list;
}
}
}
引入NUnit,进行测试
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
namespace TestUnit
{
/// <summary>
/// 单元测试类
/// </summary>
[TestFixture]
public class TestUnits
{
[Test]
public void printAdminInfo()
{
Admin ad = new Admin();
IList<string> list = ad.getAllAlbumList();
foreach (string ss in list)
{
Console.WriteLine("{0,-20},bingo!", ss);
}
}
}
}
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
namespace TestUnit
{
/// <summary>
/// 单元测试类
/// </summary>
[TestFixture]
public class TestUnits
{
[Test]
public void printAdminInfo()
{
Admin ad = new Admin();
IList<string> list = ad.getAllAlbumList();
foreach (string ss in list)
{
Console.WriteLine("{0,-20},bingo!", ss);
}
}
}
}
在NUnit里测试,查看“控制台”输出结果: