使用NUnit和DynamicMock进行单元测试

using  System;
using  System.Collections.Generic;
using  System.Text;
using  NUnit.Framework;
using  NUnit.Mocks;
namespace  DynamicMockDemo
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class Person
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public Person(int id, string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.Id = id;
            
this.Name = name;
        }

        
private int id;

        
public int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return id; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { id = value; }
        }

        
private string name;

        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { name = value; }
        }

    }

    
public interface IDB
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Person GetPersonById(
int id);
        IList
<Person> GetPersons();
        
void DeletePerson(int id);
    }

    [TestFixture]
    
public class Dome
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        [Test]
        
public void Test()
ExpandedSubBlockStart.gifContractedSubBlock.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);

            
//创建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;

            Person testPerson 
= db.GetPersonById(1);
            Assert.AreEqual(person1.Name, testPerson.Name);

            IList
<Person> testPersons = db.GetPersons();
            Assert.AreEqual(
2, testPersons.Count);

            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                db.DeletePerson(
3);
            }

            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Assert.AreEqual(
"删除失败", e.Message);
            }

            
        }

    }

}

转载于:https://www.cnblogs.com/xhan/archive/2009/06/02/1494766.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值