单元测试类,涉及通过工厂问题和注意事项创建实体

时间:2013-07-15 10:23:36

标签: c# unit-testing design-patterns

我有一个客户业务实体,只能通过CustomerFactory创建。但是,我对如何对使用Customer对象的类执行单元测试有疑问,因为单元测试需要在使用客户时设置Customer及其CustomerFactory。

使用CustomerFactory而不是构造函数的原因是它需要通过ICustomerValidator进行验证。但是,将验证逻辑放在构造函数中需要将ICustomerValidator注入构造函数中,这可能是不可取的。

例如,在执行ICustomerService实现的单元测试时,必须创建设置步骤,包括创建和设置Customer和CustomerFactory(以及通过ICustomerValidator进行客户验证,例如IdValidFirstName)。此外,只要在单元测试中使用Customer实例,就必须重复这些设置。

以下是经过单元测试的ICustomerService,但必须创建以下客户的设置:

我想对此实现进行单元测试

    public interface ICustomerService
    {
       bool AddCustomer(Customer customer);
    }

 public class Customer
    {
        internal Customer()
        { }

        public int Id { get; internal set; }

        public string Firstname { get; internal set; }

        public string Surname { get; internal set; }

        public DateTime DateOfBirth { get; internal set; }

        public string EmailAddress { get; internal set; }

        public bool HasCreditLimit { get; internal set; }

        public Company Company { get; internal set; }

        //... other properties
}

请注意,ICustomerService和ICustomerFactory的实现都取决于ICustomerValidator

     public interface ICustomerFactory
        {
            Customer CreateCustomerWith(int id, string firstName, string surname, DateTime dateOfBirth, string emailAddress, Company company);        
        }

 public class CustomerFactory : ICustomerFactory
    {
        private ICustomerValidator customerValidator;
        public CustomerFactory(ICustomerValidator customerValidator)
        {
            this.customerValidator = customerValidator;
        }

        public Customer CreateCustomerWith(int id, string firstName, string surname, DateTime dateOfBirth,
            string emailAddress, Company company)
        {

        //validation logic here ...
        }
     }



 public interface ICustomerValidator
    {
        IDictionary<string, string> Errors { get; }

        bool IsValid(Customer customer);

        bool IsValidId(int id);

        bool IsValidFirstName(string firstName);

        bool IsValidSurname(string surname);

        bool IsValidDateOfBirth(DateTime dateOfBirth);

        bool IsValidEmailAddress(string emailAddress);

        bool IsValidCompany(Company company);

    }

但是我必须在执行ICustomerService的单元测试时设置它,这看起来很多设置,或者它是否正确,或者是否有更好的方法(我可以让它重复使用,但我仍然需要调用它在ICustomerService的测试中,它是唯一的方式吗?

private Customer CreatCustomer()
        {
            var customerId = 0;
            var firstName = "John";
            var surname = "Tom";
            var emailAddress = "John.Tom@unit.com";
            var dateOfBirth = new DateTime(2013, 1, 1);
            var company = new Company(34, "Unit", Classification.VIP);
            SetupForCustomerCreation(customerId,firstName,surname,emailAddress,dateOfBirth,company);

            var customer = this.customerFactory
                                        .CreateCustomerWith(
                                            customerId, firstName, surname, dateOfBirth, emailAddress, company);

            return customer;
        }

        private void SetupForCustomerCreation(int customerId, string firstName, string surname, string emailAddress, DateTime dateOfBirth, Company company)
        {
            this.customerValidatorMock.Setup(c => 
                                                c.IsValidId(customerId)).Returns(true);
            this.customerValidatorMock.Setup(c => 
                                                c.IsValidFirstName(firstName)).Returns(true);
            this.customerValidatorMock.Setup(c => 
                                                c.IsValidSurname(surname)).Returns(true);
            this.customerValidatorMock.Setup(c => 
                                                c.IsValidDateOfBirth(dateOfBirth)).Returns(true);
            this.customerValidatorMock.Setup(c => 
                                                c.IsValidEmailAddress(emailAddress)).Returns(true);
            this.customerValidatorMock.Setup(c => 
                                                c.IsValidCompany(company)).Returns(true);
        }

提前致谢!

2 个答案:

答案 0 :(得分:1)

我建议将Customer构造函数公开。完成后,您可以自由使用它,而无需一直触摸它的工厂。通过将非公共建设者引入某个类使其隐含地依赖于其工厂,这在大多数情况下是不推荐的。最好是松散耦合的类,因为它更容易保持和测试

答案 1 :(得分:0)

虽然我强烈建议@ Alexandr-Mihalciuc回答正确的方法,但您可以将客户类定义为InternalsVisible的程序集公开给您的测试程序集,并且能够创建客户类的实例在测试中,即使你保留了你的内部人员。