我如何测试无法引用相同 属性 的情况?
How do i test a case where i cant reference to the same property?
测试用例:当我编辑客户时,存储库由于某种原因无法更新,服务应该 return 这个异常。
测试方法:
public bool EditCustomer(CustomerViewModel customerToEdit)
{
return _repository.Update(customerToEdit.ToEntity());
}
[TestFixtureSetUp]
public void SetUp()
{
this._customerRepositoryMock = Substitute.For<ICustomerRepository>();
this._customerService = new CustomerService(this._customerRepositoryMock);
}
[Test]
public void EditCustomerShouldReturnTrueWhenCustomerIsCreated()
{
var c = new CustomerViewModel();
_customerRepositoryMock.Update(c.ToEntity()).Returns(x => {throw new Exception();});
Assert.Throws<Exception>(() => _customerService.EditCustomer(c));
}
这个测试用例当然不会工作,因为 customerToEdit.ToEntity() != c.ToEntity() 因为它们不引用同一个对象。有什么办法可以测试这个案例吗?或者我应该重写整个应用程序,并让控制器负责实体之间的转换?
我不确定您使用的是哪个库,但是使用 Moq 可以通过多种方式实现:
var viewModel = new CustomerViewModel();
var customerRepositoryMock = new Mock<ICustomerRepository>();
// If you just want to test out the behavior of an exception being thrown,
// regardless of what is passed in
customerRepositoryMock
.Setup(r => r.Update(It.IsAny<CustomerViewModel>()))
.Throws<Exception>();
// If you need to throw an exception when the viewmodel contains certain properties
customerRepositoryMock
.Setup(r => r.Update(It.Is<CustomerViewModel>(c => c.Id == viewModel.Id)))
.Throws<Exception>();
// If you need to throw an exception with specific properties, and verify those
customerRepositoryMock
.Setup(r => r.Update(It.Is<CustomerViewModel>(c => c.Id == viewModel.Id)))
.Throws(new Exception("some message"));
然后您可以执行您已经定义的断言。
我在这里省略了您的 ToEntity()
方法,因为重点是向您展示更多 "loose" 确定输入参数是否相等的方法,例如按类型或按属性,而不仅仅是按引用.
测试用例:当我编辑客户时,存储库由于某种原因无法更新,服务应该 return 这个异常。
测试方法:
public bool EditCustomer(CustomerViewModel customerToEdit)
{
return _repository.Update(customerToEdit.ToEntity());
}
[TestFixtureSetUp]
public void SetUp()
{
this._customerRepositoryMock = Substitute.For<ICustomerRepository>();
this._customerService = new CustomerService(this._customerRepositoryMock);
}
[Test]
public void EditCustomerShouldReturnTrueWhenCustomerIsCreated()
{
var c = new CustomerViewModel();
_customerRepositoryMock.Update(c.ToEntity()).Returns(x => {throw new Exception();});
Assert.Throws<Exception>(() => _customerService.EditCustomer(c));
}
这个测试用例当然不会工作,因为 customerToEdit.ToEntity() != c.ToEntity() 因为它们不引用同一个对象。有什么办法可以测试这个案例吗?或者我应该重写整个应用程序,并让控制器负责实体之间的转换?
我不确定您使用的是哪个库,但是使用 Moq 可以通过多种方式实现:
var viewModel = new CustomerViewModel();
var customerRepositoryMock = new Mock<ICustomerRepository>();
// If you just want to test out the behavior of an exception being thrown,
// regardless of what is passed in
customerRepositoryMock
.Setup(r => r.Update(It.IsAny<CustomerViewModel>()))
.Throws<Exception>();
// If you need to throw an exception when the viewmodel contains certain properties
customerRepositoryMock
.Setup(r => r.Update(It.Is<CustomerViewModel>(c => c.Id == viewModel.Id)))
.Throws<Exception>();
// If you need to throw an exception with specific properties, and verify those
customerRepositoryMock
.Setup(r => r.Update(It.Is<CustomerViewModel>(c => c.Id == viewModel.Id)))
.Throws(new Exception("some message"));
然后您可以执行您已经定义的断言。
我在这里省略了您的 ToEntity()
方法,因为重点是向您展示更多 "loose" 确定输入参数是否相等的方法,例如按类型或按属性,而不仅仅是按引用.