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

  • Home
  • SEARCH
  • 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 9085963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:22:23+00:00 2026-06-16T21:22:23+00:00

I am new to MVC and Unit Test. I am using Visual Studio unit

  • 0

I am new to MVC and Unit Test. I am using Visual Studio unit test framework to test a product controller. The controller works on actual website but it always returns null in Unit Test. I have switch around keyword such as ViewResult or name of Model but all of them doesn’t work because controller always return null. Then I put breaking point and start debug the testing code. When I test it in Immediate Window it has value.

What am I missing here?

I have a screenshot on this URL: https://i.stack.imgur.com/Um2Ec.png
In screenshot you can see the Immediate Window the model is passed from controller successful, P4 P5 is stored in result. It just not work in UnitTest.

The List code is quite simple, It works fine on website. here is the code

    public int PageSize = 4;
    private IProductRepository repository;

    public ProductController(IProductRepository productRepository)
    {
        repository = productRepository;
    }

    public ViewResult List(int page=1)
    {
        ProductsListViewModel viewModel = new ProductsListViewModel
        {
            Products = repository.Products
            .OrderBy(p => p.ProductID)
            .Skip((page - 1) * PageSize)
            .Take(PageSize).AsEnumerable(),
            PagingInfo = new PagingInfo
            {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = repository.Products.Count()
            }
        };

        return View(viewModel);
    }
  • 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-16T21:22:24+00:00Added an answer on June 16, 2026 at 9:22 pm

    Had this problem before, you are not using the same versions of System.Web.Mvc in your projects. You must reference the same version of System.Web.Mvc both in your main project and unit test project.

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

Sidebar

Related Questions

I'm new to unit testing in general, but wanted to implement it in MVC
I am new to test driven development, and trying to unit test an mvc
I'm an experienced programmer, but new to LINQ/Moq/Ninject/MVC/MS Test/etc and have run into an
I'm trying to unit test a controller, but can't figure out how to pass
I create an ASP.NET MVC 3 project and a unit test project using xUnit.net.
So I'm trying to unit-test a controller method. I'm using MSTest in VS 2010,
How do I Unit Test a MVC redirection? public ActionResult Create(Product product) { _productTask.Save(product);
I'm only new to Unit Testing and ASP.NET MVC. I've been trying to get
I'm just getting going with ASP.NET MVC and I'm also new to unit testing
I am new with MVC I am trying to create a small product form

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.