gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:24 24 4
gpt4 key购买 nike

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

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

例如,在对 ICustomerService 的实现执行单元测试时,必须创 build 置步骤,包括创建和设置 Customer 和 CustomerFactory(加上通过 ICustomerValidator 进行的客户验证,例如 IdValidFirstName)。此外,每当在单元测试中使用 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 执行单元测试时设置它,这似乎有很多设置,或者它是否正确,或者是否有更好的方法(我可以重新使用它,但我仍然必须调用它在 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);
}

提前致谢!

最佳答案

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

关于c# - 通过工厂问题和注意事项涉及实体创建的单元测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17651971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com