2

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

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

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

下面是正在单元测试的 ICustomerService,但必须创建下面的 Customer 设置:

我想对这个实现进行单元测试

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

提前致谢!

4

2 回答 2

1

我建议将 Customer 构造函数公开。这样做之后,您可以自由使用它,而无需一直触摸它的工厂。通过向类引入非公共构造函数,使其隐式依赖于其工厂,这在大多数情况下是不推荐的。最好有松散耦合的类,因为它更容易维护和测试

于 2013-07-15T10:32:45.180 回答
0

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

于 2013-07-15T16:49:57.670 回答