using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using NUnit.Mocks;
namespace DynamicMockDemo
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class Person
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Person(int id, string name)
![](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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return id; }
![](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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return name; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ name = value; }
}
}
public interface IDB
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Person GetPersonById(int id);
IList<Person> GetPersons();
void DeletePerson(int id);
}
[TestFixture]
public class Dome
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[Test]
public void Test()
![](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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
db.DeletePerson(3);
}
catch (Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Assert.AreEqual("删除失败", e.Message);
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted on
2009-06-02 17:54
xhan
阅读(
1558)
评论()
编辑
收藏
举报