我开始使用TDD(和MoQ)。我有一个方法接受订单并为该订单创建唯一的CustomerId。测试某种方法,并引发异常
public class CustomerService
{
public CustomerService(IOrderRepository orderRepo)
{
_orderRepo = orderRepo;
}
public string Create(Order order)
{
//1. Save the Order that comes in, irrespective of being valid or not.
_orderRepo.Save(order);
//2. If the order is invalid, throw an exception.
if (!isValid(order))
throw new ArgumentException();
//3. Create a Customer, generate a unique CustomerId and return that.
return createCustomerAndGenerateCustomerId(order);
}
}
下面的测试似乎并不正确工作:
[Test]
[ExpectedException(typeof(ArgumentException))]
public void Create_WhenPassedInvalidOrder_StillPersistsIt()
{
//Arrange
var order = new Order(); //This is invalid, as it has some mandatory fields missing
var mockOrderRepo = new Mock<IOrderRepository>();
var customerService = new CustomerService(mockOrderRepo.Object);
//Act
customerService.Create(order); //This should call Save() on the order repo, but still throw an exception.
//Assert
mockOrderRepo.Verify(o => o.Save(order), Times.Once());
}
这个测试总是通过,即使我不叫_orderRepo.Save()。我究竟做错了什么?
你必须决定你想在您的测试断言什么:你想断言抛出了一个异常,还是你想断言你的'Save'方法被调用,因为在使用'ExpectedException'属性时你不能同时执行这两个操作...... – nemesv
如果我尝试调试,程序执行并没有停止抛出新的ArgumentException(),而是继续沿着这个方法。这是由于ExpectedException? – Narayana