【发布时间】:2012-10-02 16:03:54
【问题描述】:
我是 Mocking 的新手,但这一定是我缺少的一些非常基本的东西:
下面的测试代码产生异常:
预期至少对模拟调用一次,但从未执行过:x => x.DeleteProducts(._products)\r\n\r\n已配置设置:\r\nx => x.DeleteProducts(._products) , Times.Never\r\n\r\n执行的调用:\r\nIProductRepository.DeleteProducts(System.Collections.Generic.List`1[WebApiDemo.DataAccessLayer.Product])
我单步执行控制器方法,它似乎调用了 DeleteProducts 方法...
// Arrange
IEnumerable<Product> _products = Helpers.ProductHelpers.CreateProducts(_numberProducts);
Mock<IProductRepository> _productRepository = new Mock<IProductRepository>();
_productRepository.Setup(x => x.DeleteProducts(_products));
ProductsController controller = new ProductsController(_productRepository.Object);
// Act
controller.Destroy(_productViewModels); // Destroy calls DeleteProducts
// Assert
_productRepository.Verify(x => x.DeleteProducts(_products));
【问题讨论】:
-
我重新格式化了你的问题 - 它不是特别容易阅读。 (编辑待定...)
标签: moq