我有一个只能通过 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);
}
提前致谢!