本文参考

http://www.cnblogs.com/haogj/archive/2011/06/24/2088788.html

Moq适合于TDD的项目,小项目初期应该不太适合使用,有些浪费时间了!

NuGet 分别添加 NUnit 和 Moq

install-package nunit -project 测试项目名称

install-package moq -project 测试项目名称

public class Person
{
public string Id;
public string FirstName;
public string LastName;
public Person(string newId, string fn, string ln)
{
Id = newId;
FirstName = fn;
LastName = ln;
}
} public interface IPersonRepository
{
List<Person> GetPeople();
Person GetPersonById(string id);
} public class PersonService
{
private IPersonRepository personRepos;
public PersonService(IPersonRepository repos)
{
personRepos = repos;
}
public List<Person> GetAllPeople()
{
return personRepos.GetPeople();
}
public List<Person> GetAllPeopleSorted()
{
List<Person> people = personRepos.GetPeople();
people.Sort(delegate(Person lhp, Person rhp)
{
return lhp.LastName.CompareTo(rhp.LastName);
});
return people;
}
public Person GetPerson(string id)
{
try
{
return personRepos.GetPersonById(id);
}
catch (ArgumentException)
{
return null; // no person with that id was found
}
}
}

Moq

[TestFixture]
public class NUnitTest
{ private Mock<IPersonRepository> mo = new Mock<IPersonRepository>(); private Person onePerson = new Person("", "Wendy", "Whiner");
private Person secondPerson = new Person("", "Aaron", "Adams");
private List<Person> peopleList; /// <summary>
/// 填充基础数据
/// </summary>
[SetUp]//每个测试方法被调用之前执行
[Category("Mock")]
public void Init()
{
peopleList = new List<Person>();
peopleList.Add(onePerson);
peopleList.Add(secondPerson); } [Test]
[Category("Mock")]
public void TestGetAllPeople()
{
//模拟使用接口方法及返还数据
mo.Setup(m => m.GetPeople()).Returns(peopleList); PersonService service = new PersonService(mo.Object); Assert.AreEqual(, service.GetAllPeople().Count);
} [Test]
[Category("Mock")]
public void TestGetAllPeopleSorted()
{
mo.Setup(m => m.GetPeople()).Returns(peopleList); PersonService service = new PersonService(mo.Object); var p = service.GetAllPeopleSorted()[]; Assert.AreEqual("Adams", p.LastName);
} [Test]
[Category("Mock")]
public void TestGetSinglePersonWithValidId()
{
mo.Setup(m => m.GetPersonById(It.Is<string>(s => s == "")))
.Returns(onePerson); PersonService service = new PersonService(mo.Object); var p = service.GetPerson(""); Assert.IsNotNull(p);
Assert.AreEqual(p.Id, "");
} [Test]
[Category("Mock")]
public void TestGetSinglePersonWithInalidId()
{
mo.Setup(m => m.GetPersonById(It.IsAny<string>())); PersonService service = new PersonService(mo.Object);
Assert.IsNull(service.GetPerson(null));
} }

最新文章

  1. MySQL生成模型
  2. 在Gradle中使用jaxb的xjc插件
  3. python 基本数据类型分析
  4. IE兼容性
  5. iframe自适应高度js
  6. js中的DOM操作(1)
  7. C++程序结构---1
  8. DG_Oracle DataGuard作用和概念(概念)
  9. Keil RTX systick 初始化
  10. 51nod1476 括号序列的最小代价
  11. QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码
  12. hibernate级联保存,更新个人遇到的问题
  13. Selenium2Library关键字
  14. IIS日志字段详解
  15. htmlparser 学习
  16. Docker安装Tomcat镜像并部署web项目
  17. orcal10g下载地址
  18. MongoDB可视化工具--Robo 3T 使用教程
  19. UVa - 12050
  20. 远程客户端连接MysqL数据库太慢解决方案

热门文章

  1. C之typedef应用
  2. Git服务器搭建--ssh/http
  3. PAT甲级 链表题_C++题解
  4. 华为模拟机试_C++题解
  5. csdn博客整理
  6. python3+django报错testserver
  7. WUSTOJ 1327: Lucky Numbers(Java)
  8. ELK基础配置
  9. VS.NET(C#)--2.9_HTML服务器控件案例
  10. C#Modbus Rtu的实现