Nunit

一. Mock

1. PersonService

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
        }
    }
}

 

 

2. Test

 

[TestFixture]
public class PersonServiceTest
{
    // The dynamic mock proxy that we will use to implement IPersonRepository
    private DynamicMock personRepositoryMock;

    // Set up some testing data
    private Person onePerson = new Person("1", "Wendy", "Whiner");
    private Person secondPerson = new Person("2", "Aaron", "Adams");

    private List<Person> peopleList;

    [SetUp]
    public void TestInit()
    {
        peopleList = new List<Person>();
        peopleList.Add(onePerson);
        peopleList.Add(secondPerson);

        // Construct a Mock Object of the IPersonRepository Interface
        personRepositoryMock = new DynamicMock(typeof(IPersonRepository));
    }

    [Test]
    public void TestGetAllPeople()
    {
        // Tell that mock object when the "GetPeople" method is
        // called to return a predefined list of people
        personRepositoryMock.ExpectAndReturn("GetPeople", peopleList); //core function mock!!

        // Construct a Person service with the Mock IPersonRepository
        PersonService service = new PersonService(
             (IPersonRepository)personRepositoryMock.MockInstance);

        // Call methods and assert tests
        Assert.AreEqual(2, service.GetAllPeople().Count);
    }

    [Test]
    public void TestGetAllPeopleSorted()
    {
        // Tell that mock object when the "GetPeople" method is called to
        // return a predefined list of people
        personRepositoryMock.ExpectAndReturn("GetPeople", peopleList);

        PersonService service = new PersonService(
                (IPersonRepository)personRepositoryMock.MockInstance);

        // This method really has "business logic" in it - the sorting of people
        List<Person> people = service.GetAllPeopleSorted();
        Assert.IsNotNull(people);
        Assert.AreEqual(2, people.Count);

        // Make sure the first person returned is the correct one
        Person p = people[0];
        Assert.AreEqual("Adams", p.LastName);
    }

    [Test]
    public void TestGetSinglePersonWithValidId()
    {
        // Tell that mock object when the "GetPerson" method is called to
        // return a predefined Person
        personRepositoryMock.ExpectAndReturn("GetPersonById", onePerson, "1");

        PersonService service = new PersonService(
            (IPersonRepository)personRepositoryMock.MockInstance);

        Person p = service.GetPerson("1");
        Assert.IsNotNull(p);
        Assert.AreEqual(p.Id, "1");
    }

    [Test]
    public void TestGetSinglePersonWithInalidId()
    {
        // Tell that mock object when the "GetPersonById" is called with a null
        // value to throw an ArgumentException
        personRepositoryMock.ExpectAndThrow("GetPersonById",
             new ArgumentException("Invalid person id."), null);

        PersonService service = new PersonService(
                 (IPersonRepository)personRepositoryMock.MockInstance);

        // The only way to get null is if the underlying IPersonRepository
        // threw an ArgumentException
        Assert.IsNull(service.GetPerson(null));
    }
}

 

二.  TestCase Attribute

[TestCase(12,3,4)]
[TestCase(12,2,6)]
[TestCase(12,4,3)]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual( q, n / d );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值