Moq 测试是否在 returns void 的方法中更改了属性
Moq test if an attribute was changed in a method that returns void
我想知道是否可以使用 Moq 进行测试,如果对象中的某些属性被某种方法更改,则该方法必须无效。
我想检查产品的属性 "FlgActive" 是否已从 true 更改为 false。
delete 方法无效,它只是将该标志更改为 false。
如果可能的话,我应该在我的设置中添加什么来检查?
示例:
测试
[TestMethod]
public void DeletedProductFlagShouldBeFalse() //RN03
{
var product = new Product() { Id = 1, Name = "aaaaaa", Code = "aaaaaa", FlgActive = true };
var so = new ServiceOrder() { };
var mockProductRepository = new Mock<IProductRepository>();
mockProductRepository.Setup(p => p.Delete(product));
var mockServiceOrderRepository = new Mock<IServiceOrderRepository>();
mockServiceOrderRepository.Setup(o => o.GetProductById(product.Id)).Returns(new List<ServiceOrder> { so });
var service = new ProductService(mockProductRepository.Object, mockServiceOrderRepository.Object);
service.Delete(product);
mockProductRepository.Verify(c => c.Delete(It.IsAny<Product>()), Times.Once());
mockProductRepository.Verify((s => s.Commit()), Times.Once());
Assert.IsFalse(product.FlgActive);
}
服务
public void Delete(Product product)
{
Validate(product);
repo.Delete(product);
repo.Commit();
}
存储库
public void Delete(Product product)
{
try
{
product.FlgActive = false;
Update(product);
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
将标志设置为 false 的不是您的服务,因此您不应从您的服务中测试该行为。该单元的工作是验证产品,然后将删除委托给存储库。
因此您可以从测试中删除该部分并重命名它:
[TestMethod]
public void DeletionShouldBeSentToRepository() //RN03
{
var product = new Product() { Id = 1, Name = "aaaaaa", Code = "aaaaaa", FlgActive = true };
var so = new ServiceOrder() { };
var mockProductRepository = new Mock<IProductRepository>();
mockProductRepository.Setup(p => p.Delete(product));
var mockServiceOrderRepository = new Mock<IServiceOrderRepository>();
mockServiceOrderRepository.Setup(o => o.GetProductById(product.Id)).Returns(new List<ServiceOrder> { so });
var service = new ProductService(mockProductRepository.Object, mockServiceOrderRepository.Object);
service.Delete(product);
mockProductRepository.Verify(c => c.Delete(product), Times.Once());
mockProductRepository.Verify((s => s.Commit()), Times.Once());
}
注意:我很乐意确认我们将优质产品发送到仓库。我已将您的 It.IsAny
替换为您之前创建的产品。
既然我们已经涵盖了您服务的职责,我们可以测试存储库本身的删除行为,它应该如下所示:
[TestMethod]
public void DeletedProductFlagShouldBeFalse() //RN03
{
var product = new Product() { Id = 1, Name = "aaaaaa", Code = "aaaaaa", FlgActive = true };
IProductRepository repository = new ProductRepository();
repository.Delete(product);
Assert.IsFalse(product.FlgActive);
}
进行单元测试时,重要的是只测试当前 class 的行为,而不是副作用。
我想知道是否可以使用 Moq 进行测试,如果对象中的某些属性被某种方法更改,则该方法必须无效。
我想检查产品的属性 "FlgActive" 是否已从 true 更改为 false。
delete 方法无效,它只是将该标志更改为 false。
如果可能的话,我应该在我的设置中添加什么来检查?
示例:
测试
[TestMethod]
public void DeletedProductFlagShouldBeFalse() //RN03
{
var product = new Product() { Id = 1, Name = "aaaaaa", Code = "aaaaaa", FlgActive = true };
var so = new ServiceOrder() { };
var mockProductRepository = new Mock<IProductRepository>();
mockProductRepository.Setup(p => p.Delete(product));
var mockServiceOrderRepository = new Mock<IServiceOrderRepository>();
mockServiceOrderRepository.Setup(o => o.GetProductById(product.Id)).Returns(new List<ServiceOrder> { so });
var service = new ProductService(mockProductRepository.Object, mockServiceOrderRepository.Object);
service.Delete(product);
mockProductRepository.Verify(c => c.Delete(It.IsAny<Product>()), Times.Once());
mockProductRepository.Verify((s => s.Commit()), Times.Once());
Assert.IsFalse(product.FlgActive);
}
服务
public void Delete(Product product)
{
Validate(product);
repo.Delete(product);
repo.Commit();
}
存储库
public void Delete(Product product)
{
try
{
product.FlgActive = false;
Update(product);
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
将标志设置为 false 的不是您的服务,因此您不应从您的服务中测试该行为。该单元的工作是验证产品,然后将删除委托给存储库。
因此您可以从测试中删除该部分并重命名它:
[TestMethod]
public void DeletionShouldBeSentToRepository() //RN03
{
var product = new Product() { Id = 1, Name = "aaaaaa", Code = "aaaaaa", FlgActive = true };
var so = new ServiceOrder() { };
var mockProductRepository = new Mock<IProductRepository>();
mockProductRepository.Setup(p => p.Delete(product));
var mockServiceOrderRepository = new Mock<IServiceOrderRepository>();
mockServiceOrderRepository.Setup(o => o.GetProductById(product.Id)).Returns(new List<ServiceOrder> { so });
var service = new ProductService(mockProductRepository.Object, mockServiceOrderRepository.Object);
service.Delete(product);
mockProductRepository.Verify(c => c.Delete(product), Times.Once());
mockProductRepository.Verify((s => s.Commit()), Times.Once());
}
注意:我很乐意确认我们将优质产品发送到仓库。我已将您的 It.IsAny
替换为您之前创建的产品。
既然我们已经涵盖了您服务的职责,我们可以测试存储库本身的删除行为,它应该如下所示:
[TestMethod]
public void DeletedProductFlagShouldBeFalse() //RN03
{
var product = new Product() { Id = 1, Name = "aaaaaa", Code = "aaaaaa", FlgActive = true };
IProductRepository repository = new ProductRepository();
repository.Delete(product);
Assert.IsFalse(product.FlgActive);
}
进行单元测试时,重要的是只测试当前 class 的行为,而不是副作用。