0

次のコントローラー アクションの単体テストを作成します。

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、アクションがに適切な値を持つ をProductList返すことです。の値をテストする方法がわかりません。PartialViewproductListIEnumerable<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); ???
}

aを anSystem.InvalidCastExceptionにキャストしようとしているため、 a を取得します-これが私が立ち往生している場所です。 テストのためにコントローラーでIEnumerable をターゲットにするにはどうすればよいですか?PartialViewResultIEnumerableproductList

また、部分ビューが正しく生成されていることをテストしないのは悪い習慣でしょうか? (productList値が正しければ、部分ビューが適切にレンダリングされると想定しています)

4

1 に答える 1

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 値を持つ予定でしたが、複数のコントローラー アクションの結果を同時にテストする際に問題が発生し始めました。

于 2012-07-20T13:47:51.313 に答える