0

我想寫以下控制器動作單元測試:如何測試從Controller Action返回的PartialViewResult的對象模型?

public ActionResult ProductList(int category) 
{ 
    IEnumerable<Product> productList = repository.Products.Where(p => p.CategoryId == category); 
    return PartialView("ProductList", productList); 
} 

,這是我的看法:

@model IEnumerable<POS.Domain.Entities.Product> 

@foreach (var p in Model) 
{ 
    Html.RenderPartial("_ProductSummary", p); 
} 

我要測試的是,給出的int值category,該ProductListproductList中返回一個PartialView,其值爲合適的值。我不知道如何測試IEnumerable<Product> productList的價值。

這是到目前爲止我的單元測試:

[TestMethod] 
public void ProductListReturnsAppropriateProducts() 
{ 
    // Arrange 
    // - create the mock repository 
    Mock<IProductRepository> mock = new Mock<IProductRepository>(); 
    mock.Setup(m => m.Products).Returns(new Product[] { 
    new Product {ProductId = 1, Name = "P1", CategoryId = 1}, 
    new Product {ProductId = 2, Name = "P2", CategoryId = 2}, 
    new Product {ProductId = 3, Name = "P3", CategoryId = 1}, 
    new Product {ProductId = 4, Name = "P4", CategoryId = 2}, 
    new Product {ProductId = 5, Name = "P5", CategoryId = 3} 
    }.AsQueryable()); 

    // Arrange - create a controller 
    ProductController controller = new ProductController(mock.Object); 

    // Action 
    IEnumerable<Product> result1 = (IEnumerable<Product>)controller.ProductList(1); 
    //IEnumerable<Product> result2 = (IEnumerable<Product>)controller.ProductList(2); ??? 

    // Assert 
    Assert.AreEqual(result1, 2); 
    // Assert.AreEqual(result2, 2); ??? 
} 

我得到一個System.InvalidCastException因爲我想投下PartialViewResultIEnumerable - 這就是我卡住了。 如何將我的控制器中的IEnumerable productList作爲測試目標?

此外,不測試部分視圖是否正確生成是不好的做法? (我假設,如果productList值是正確的,部分觀點將被適當地渲染)

回答

3

我發現,似乎掩蓋什麼,我也需要一個解決方案:

/// <summary> 
/// Tests that the returned PartialViewResult contains the appropriate products for the selected category 
/// First checks that the number of products is correct 
/// Then checks that the number of products selected by a specific name is correct, to ensure that the Action did not 
/// return products from different categories with the same Product.Name 
///</summary> 
[TestMethod] 
public void ProductListReturnsAppropriateProducts() 
{ 
    // Arrange 
    // - create the mock repository 
    Mock<IProductRepository> mock = new Mock<IProductRepository>(); 
    mock.Setup(m => m.Products).Returns(new Product[] { 
     new Product {ProductId = 1, Name = "P1", CategoryId = 1}, 
     new Product {ProductId = 2, Name = "P2", CategoryId = 2}, 
     new Product {ProductId = 3, Name = "P3", CategoryId = 1}, 
     new Product {ProductId = 4, Name = "P4", CategoryId = 2}, 
     new Product {ProductId = 5, Name = "P4", CategoryId = 3} 
    }.AsQueryable()); 

    // Arrange - create a controller 
    ProductController controller = new ProductController(mock.Object); 

    // Action 
    PartialViewResult result = (PartialViewResult) controller.ProductList(2); 

    // Assert 
    Assert.AreEqual(((IEnumerable<Product>)result.ViewData.Model).Count(), 2); 
    Assert.IsTrue(((IEnumerable<Product>)result.ViewData.Model).Count(o => o.Name == "P4") == 1); 
} 

我將不得不幾個PartialViewResult值 - 但我開始有問題同時測試多個Controller Action結果。

相關問題