Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8263207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:01:19+00:00 2026-06-08T04:01:19+00:00

I want to write a unit test for the following controller action: public ActionResult

  • 0

I want to write a unit test for the following controller action:

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

and this is my view:

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

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

What I want to test is that given the int value category, the ProductList action returns a PartialView with the appropriate values in productList. I am not sure how to test the value of the IEnumerable<Product> productList.

Here is my unit test so far:

[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); ???
}

I get a System.InvalidCastException because I am trying to cast a PartialViewResult to an IEnumerable – which is where I’m stuck. How do I target the IEnumerable productList in my controller for testing?

Also, would it be bad practice to not test that the partial view is generated correctly? (I am assuming that if the productList value is correct, the partial view will be rendered appropriately)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:01:20+00:00Added an answer on June 8, 2026 at 4:01 am

    I found a solution that appears to cover what I need it to:

    /// <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);
    }
    

    I was going to have several PartialViewResult values – but I began to have issues with testing multiple Controller Action results simultaneously.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a unit test for the following class: @Transactional public
This is a function that I want to write a unit test for: public
I want to write a unit test for a Django manage.py command that does
Assume I want to write a unit test to test a particular piece of
I have defined the following Unit-Test: [TestMethod] //@Test for the Java crowd public void
I have a bunch of repository classes which I want to unit-test using Visual
I want to write an unit test case to test the code in IBAction
I do write unit tests while writing APIs and core functionalities. But I want
I'm writing some unit tests in UnitTest++ and want to write a bunch of
I'm starting to write some unit test for my javascript code using qunit and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.