2012-10-02 67 views
0

我是新來的嘲諷,但是這必須是真正的東西基本是我的思念:起訂量拋出異常的驗證

下面的測試代碼生成了異常:

預計調用上模擬至少一次,但從未執行: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)); 
+1

我重新格式化了您的問題 - 這不是特別容易閱讀。 (Edit is pending ...) – SpaceBison

回答

0

是否DeleteProducts(_products);返回空隙?我假設它,所以你需要把.Verifiable()放在.Setup()的末尾。

有了這個,它應該運行通過,雖然我不知道爲什麼你有Times.Never()而不是Times.Once()

我也提倡使用It.IsAny<T>設置呼叫,而不是特定的集合,如:

MyMock.Setup(x => x.MyMethod(It.IsAny<IEnumerable<Widget>>)).Verifiable() 
+0

我改變了_productRepository.Setup(x => x.DeleteProducts(_products)); to _productRepository.Setup(x => x.DeleteProducts(_products))。Verifiable();獲取完全相同的錯誤。 – Dave

0

除非你有仿製品的行爲設置爲嚴格沒有必要的設置。您沒有從「刪除」中返回任何內容。對驗證的呼叫就足夠了。

有些東西在代碼中並不完全明顯。

存儲庫中刪除產品但控制器破壞productviewmodels

在起訂量4如果

  • 你必須在每_產品展示產品的產品視圖模型測試應該工作
  • 的Controller.Destroy方法獲取從產品的ViewModels在相同的順序_產品展示

我會檢查_productViewModels是1:1匹配的_產品展示,並檢查如何銷燬()提取從的ViewModels產品調用delete()

我不會用IsAny>()去,因爲你想檢查這些特定的產品是否被刪除而不是其他的。

[TestClass] 
public class Verifying { 

    public interface IProductRepository { 
     void Delete(IEnumerable<Product> products); 
    } 

    public class ProductController { 
     private IProductRepository _repository; 
     public ProductController(IProductRepository repository) { 
      _repository = repository; 
     } 
     public void Destroy(IEnumerable<Product> products) { 
      _repository.Delete(products); 
     } 
     public void Destroy(IEnumerable<ProductViewModel> productViewModels) { 
      _repository.Delete(productViewModels.Select(vm => vm.Product)); 
     } 
    } 

    public class Product { 
    } 

    public class ProductViewModel { 
     public Product Product { get; set;} 
    } 


    static Verifying() { 
     sProducts = new List<Product> { new Product(), new Product(), new Product() }; 
     sProductViewModels = new List<ProductViewModel>(sProducts.Select(p => new ProductViewModel { Product = p })); 
    } 

    private static List<Product> sProducts; 
    private static List<ProductViewModel> sProductViewModels; 

    private Mock<IProductRepository> _mockRepository; 
    private ProductController CreateController() { 
     _mockRepository = new Mock<IProductRepository>(); 
     return new ProductController(_mockRepository.Object); 
    } 

    [TestMethod] 
    public void DestroyingProducts() { 
     var controller = CreateController(); 
     controller.Destroy(sProducts); 
     _mockRepository.Verify(mk => mk.Delete(sProducts)); 
    } 

    [TestMethod] 
    public void DestroyingProductViewModels() { 
     var controller = CreateController(); 
     controller.Destroy(sProductViewModels); 
     _mockRepository.Verify(mk => mk.Delete(sProducts)); 
    } 

}