using ClassLibrary1;
using NUnit.Framework;
using Rhino.Mocks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
[TestFixture]
public class CustomerRegistrationServiceTests:ServiceTest
{
private IRepository<Customer> _customerRepo;
private ICustomerService _customerService;
[SetUp]
public new void SetUp()
{
_customerRepo = MockRepository.GenerateMock<IRepository<Customer>>();
var customer1 = new Customer {
Name="davidmaster"
};
//我们希望当调用仓储的Tabel方法时返回的是包含customer1的列表数据
_customerRepo.Expect(x => x.Table).Return(new List<Customer> { customer1 }.AsQueryable());
_customerService = new CustomerService(_customerRepo);
}
[Test]
public void Ensure_only_registered_customer_can_login()
{
var result = _customerService.ValidateCustomer("davidmaster");
result.ShouldEqual(true);
}
}
public class Customer
{
public string Name;
}
}
NUnit+Rhino.Mocks在.net的研发中,我们一般采用NUnit+Rhino.Mocks的双组合框架实现测试用例的编写。对于常用的Service发放的单元测试,我们一般需要建立一个抽象的ServiceTest来做一些公共的事情。比如Mock一些公共的仓储,应用程序上下文等。namespace ClassLibrary1{ [TestFixture] pu