最小起订量和获取异常
moq and fetching an exception
我正在尝试通过一些简单的示例来学习最小起订量测试
我有
public class CustomerService
{
private readonly ICustomerRepository _repository;
public CustomerService(ICustomerRepository repository)
{
_repository = repository;
}
public void CreateWithMoney(CustomerDTO dto)
{
var cust = new Customer { FirstName = dto.FirstName, LastName = dto.LastName, FinacialStatus = dto.FinacialStatus };
if (cust.FinacialStatus < 500)
{
throw new NotEnoughMoneyException();
}
_repository.Save(cust);
}
}
[Test]
public void ThrowExceptionIfMoneyIsLessThan500()
{
var mockRepo = new Mock<ICustomerRepository>();
var mockService = new Mock<CustomerService>(mockRepo.Object);
mockService.Setup(x => x.CreateWithMoney(It.IsAny<CustomerDTO>()))
.Throws<NotImplementedException>();
}
我不知道如何验证这是真的,最重要的是我无法 运行 测试,
cause I'm getting this error Expected:
EntityTest.TDD.NotEnoughMoneyException But was:
System.NotSupportedException : Invalid setup on a non-virtual
(overridable in VB) member: x =>
x.CreateWithMoney(It.IsAny())
您不能模拟 CreateWithMoney
方法,因为它不是虚拟的。为其添加virtual
关键字:
public virtual void CreateWithMoney(CustomerDTO dto)
{
...
}
如果你想测试当钱少于500时抛出异常,你的单元测试有更好的解决方案:
[Test]
[ExpectedException(typeof(NotEnoughMoneyException))]
public void ThrowExceptionIfMoneyIsLessThan500()
{
var repository = new Mock<ICustomerRepository>().Object;
var dto = ...; // Instantiate CustomerDTO that returns a value > 500 for FinancialStatus property
var target = new CustomerService(repository);
target.CreateWithMoney(dto);
}
如您所见,ExpectedException
属性指定了您期望的异常类型。查看 here 了解更多信息。
我正在尝试通过一些简单的示例来学习最小起订量测试 我有
public class CustomerService
{
private readonly ICustomerRepository _repository;
public CustomerService(ICustomerRepository repository)
{
_repository = repository;
}
public void CreateWithMoney(CustomerDTO dto)
{
var cust = new Customer { FirstName = dto.FirstName, LastName = dto.LastName, FinacialStatus = dto.FinacialStatus };
if (cust.FinacialStatus < 500)
{
throw new NotEnoughMoneyException();
}
_repository.Save(cust);
}
}
[Test]
public void ThrowExceptionIfMoneyIsLessThan500()
{
var mockRepo = new Mock<ICustomerRepository>();
var mockService = new Mock<CustomerService>(mockRepo.Object);
mockService.Setup(x => x.CreateWithMoney(It.IsAny<CustomerDTO>()))
.Throws<NotImplementedException>();
}
我不知道如何验证这是真的,最重要的是我无法 运行 测试,
cause I'm getting this error Expected: EntityTest.TDD.NotEnoughMoneyException But was: System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: x => x.CreateWithMoney(It.IsAny())
您不能模拟 CreateWithMoney
方法,因为它不是虚拟的。为其添加virtual
关键字:
public virtual void CreateWithMoney(CustomerDTO dto)
{
...
}
如果你想测试当钱少于500时抛出异常,你的单元测试有更好的解决方案:
[Test]
[ExpectedException(typeof(NotEnoughMoneyException))]
public void ThrowExceptionIfMoneyIsLessThan500()
{
var repository = new Mock<ICustomerRepository>().Object;
var dto = ...; // Instantiate CustomerDTO that returns a value > 500 for FinancialStatus property
var target = new CustomerService(repository);
target.CreateWithMoney(dto);
}
如您所见,ExpectedException
属性指定了您期望的异常类型。查看 here 了解更多信息。