单元测试流程

NUnit+Rhino.Mocks
在.net的研发中,我们一般采用NUnit+Rhino.Mocks的双组合框架实现测试用例的编写。
对于常用的Service发放的单元测试,我们一般需要建立一个抽象的ServiceTest来做一些公共的事情。比如Mock一些公共的仓储,应用程序上下文等。
[TestFixture]
    public abstract class ServiceTest
    {
        /// 
        /// 在测试用例执行前执行
        /// 
        [SetUp]
        public void SetUp()
        {
            this.InitSetUp();
        }
        /// 
        /// 在测试用例执行后执行
        /// 
        [TearDown]
        public void TearDown()
        {

        }
        private void InitSetUp()
        { }
    }

测试用例

[TestFixture]
    public class CustomerRegistrationServiceTests:ServiceTest
    {
        private IRepository _customerRepo;
        private ICustomerService _customerService;
        [SetUp]
        public new void SetUp()
        {
            _customerRepo = MockRepository.GenerateMock>();
            var customer1 = new Customer {
                Name="davidmaster"
            };
            //我们希望当调用仓储的Tabel方法时返回的是包含customer1的列表数据
            _customerRepo.Expect(x => x.Table).Return(new List { customer1 }.AsQueryable());

            _customerService = new CustomerService(_customerRepo);
        }
        [Test]
        public void Ensure_only_registered_customer_can_login()
        {
            var result = _customerService.ValidateCustomer("davidmaster");
            result.ShouldEqual(true);
        }
    }


你可能感兴趣的:(单元测试流程)