using
System;
using
System.Collections.Generic;
using
System.Text;
using
NUnit.Framework;
using
NUnit.Mocks;
namespace
DynamicMockDemo
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class Person
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Person(int id, string name)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Id = id;
this.Name = name;
}
private int id;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Id
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return id; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ id = value; }
}
private string name;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Name
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return name; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ name = value; }
}
}
public interface IDB
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Person GetPersonById(int id);
IList<Person> GetPersons();
void DeletePerson(int id);
}
[TestFixture]
public class Dome
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[Test]
public void Test()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Person person1 = new Person(1,"aaa");
Person person2 = new Person(2,"bbb");
IList<Person> persons = new List<Person>();
persons.Add(person1);
persons.Add(person2);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//创建Mock对象
DynamicMock dbMock = new DynamicMock(typeof(IDB));
//模拟GetPersonById方法,传入1,返回person1
dbMock.ExpectAndReturn("GetPersonById", person1, 1);
//模拟GetPersons,返回persons
dbMock.SetReturnValue("GetPersons", persons);
//模拟 DeletePerson,传入3,抛出异常
dbMock.ExpectAndThrow("DeletePerson", new Exception("删除失败"), 3);
//获取IDB接口的Mock对象实例
IDB db = (IDB)dbMock.MockInstance;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Person testPerson = db.GetPersonById(1);
Assert.AreEqual(person1.Name, testPerson.Name);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
IList<Person> testPersons = db.GetPersons();
Assert.AreEqual(2, testPersons.Count);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
db.DeletePerson(3);
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Assert.AreEqual("删除失败", e.Message);
}
}
}
}
转载于:https://www.cnblogs.com/xhan/archive/2009/06/02/1494766.html