单元测试可以通过接受布尔值作为开关来执行双重任务,还是应该编写两个单独的测试来复制测试代码?

Pin*_*ong 6 c# double unit-testing boolean

我有一个方法,其操作依赖于其依赖,如下所示.做单元测试仍然有价值吗?因为单元测试不是测试任何业务逻辑,而是测试模拟.

单元测试如下:

请注意,方法的操作expectedCustomerValidality由测试确定,由测试设置.大多数情况下,逻辑是由模拟决定的(例如Setup(c => c.IsValid()).

        [Test]
        [TestCase(true)]
        [TestCase(false)]
        public void AddCustomer(bool expectedCustomerValidality)
        {
           //using Moq
            companyRepositoryMock.Setup(c => c.GetById(It.IsAny<int>())).Returns(new Company());          
            customerValidatorMock.Setup(c => c.IsValid(It.IsAny<Customer>())).Returns(expectedCustomerValidality);

            var customer = new Customer
                               {
                                   Firstname = "firstName",
                                   Surname = "surname",
                                   Company = new Company { Id = 1 }
                               };

            var addCustomer = customerServiceSut.AddCustomer(customer);

            Assert.AreEqual(expectedCustomerValidality,addCustomer);
        }
Run Code Online (Sandbox Code Playgroud)

生产代码如下:

 public class CustomerService : ICustomerService
    {
        private ICompanyRepository companyRepository;
        private ICustomerRepository customerRepository;
        private ICustomerValidator customerValidator;

        public CustomerService(ICompanyRepository companyRepository, ICustomerRepository customerRepository, ICustomerValidator customerValidator)
        {
            this.companyRepository = companyRepository;
            this.customerRepository = customerRepository;
            this.customerValidator = customerValidator;

        }

        public bool AddCustomer(Customer customer)
        {           
            customer.Company = companyRepository.GetById(customer.Company.Id); ;

            if (customerValidator.IsValid(customer))
            {
                customerRepository.AddCustomer(customer);

                return true;
            }

            return false;
        }
}
Run Code Online (Sandbox Code Playgroud)

问题:

  • AddCustomer()是否需要进行单元测试?
  • 如果是,那么当前的单元测试是否正确执行?

    1如果没有,对它进行单元测试的正确方法是什么?

Rob*_*vey 7

我不喜欢这个.原因如下:您的测试方法名称应该反映测试中的方法,测试的前提条件是什么,以及您期望测试结果如何:

public bool AddCustomer_CustomerIsValid_ShouldReturnTrue()

public bool AddCustomer_CustomerIsInvalid_ShouldReturnFalse()
Run Code Online (Sandbox Code Playgroud)

现在,如果您愿意,可以将核心测试逻辑重构为自己的方法以消除代码重复,然后从上述两种方法中调用该方法.但重构的方法不是测试用例; 它只是实际测试用例的辅助方法.

例:

[Test]
public void AddCustomer_CustomerIsValid_ShouldReturnTrue()
{
    var result = AddCustomerTest(true);
    Assert.IsTrue(result);
}

[Test]
public void AddCustomer_CustomerIsInvalid_ShouldReturnFalse()
{
    var result = AddCustomerTest(false);
    Assert.IsFalse(result);
}

public void AddCustomerTest(bool expectedCustomerValidality)
{
   //using Moq
    companyRepositoryMock.Setup(c => c.GetById(It.IsAny<int>())).Returns(new Company());          
    customerValidatorMock.Setup(c => c.IsValid(It.IsAny<Customer>())).Returns(expectedCustomerValidality);

    var customer = new Customer
    {
        Firstname = "firstName",
        Surname = "surname",
        Company = new Company { Id = 1 }
    };

    var result= customerServiceSut.AddCustomer(customer);

    return result;
}
Run Code Online (Sandbox Code Playgroud)