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

本文关键字:创建 实体 单元 测试类 单元测试 工厂 问题 注意事项 | 更新日期: 2023-09-27 18:22:34

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

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

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

下面是正在进行单元测试的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执行单元测试时设置它,这似乎有很多设置,或者它是正确的,或者有更好的方法吗(我可以让它重复使用,但我仍然必须在ICustomerServices的测试中调用它,这是唯一的方法吗)?

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

提前感谢!

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

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

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