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 8849093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:33:41+00:00 2026-06-14T12:33:41+00:00

Here’s my scenario: I’m working with a .NET MVC 4.0 project, with a repository

  • 0

Here’s my scenario:

I’m working with a .NET MVC 4.0 project, with a repository (as you’d expect), and trying to implement the Moq/xUnit testing libraries into a .NET Unit Testing project.

I’ve got this far:

MVC Controller

private IHOLService _service;

public PolicyController(IHOLService service)
{
      _service = service;
}

public ActionResult Index()
{
     var policies = _service.GetAllPolicies(100, 0).ToList();

     return View(policies);
}

Unit testing class

[Fact]
 public void GetPolicies()
 {
            // Arrange
            var mockService = new Mock<IHOLService>();
            List<Policy> policy = new List<Policy>()
            mockService.Setup(cr => cr.GetAllPolicies(10, 0)).Returns(policy);
            var controller = new PolicyController(mockService.Object);

            // policy here contains 0 results.

             // Act
        var result = (ViewResult)controller.Index();

        var model = result.ViewData.Model; // equals 0.

        // Assert
        var listCategories = Assert.IsAssignableFrom<List<Policy>>(result.ViewData.Model);

         // listCategories.Count equals 0 results.

        Assert.Equal(10, listCategories.Count); // Thus always fails
  }

My problem is that when the Controller is called directly, everything works fine, 100 policies are loaded.

However, when I run the test, 0 products are loaded, in which I’m guessing is a problem with the mocking calls somewhere down the line, potentially to do with the service initialisation. Has anyone ever had this before and can offer advice?

Also, am I correct to test my Service, rather than my Repository held at data layer?

Thanks in advance.

  • 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-14T12:33:42+00:00Added an answer on June 14, 2026 at 12:33 pm

    In your test code, you initialize policy to an empty list, then tell your mock service to return this empty list. To make the test load policies, you need to put some policy instances into your policy list.

    I would write a test which looks something like this:

    [Fact]
    public void GetPolicies()
    {
       // Arrange
       var mockService = new Mock<IHOLService>();
       Policy expectedPolicy = new Policy(); // substitute for the real way you construct these
       List<Policy> policy = new List<Policy>() { expectedPolicy };
       mockService.Setup(cr => cr.GetAllPolicies(10, 0)).Returns(policy);
    
       // Act
       var result = (ViewResult)controller.Index();
    
       var model = result.ViewData.Model; // equals 0.
    
       // Assert
       var listCategories = Assert.IsAssignableFrom<List<Policy>>(result.ViewData.Model);
    
       Assert.Equal(expectedPolicy, listCategories.First());        
    }
    

    But it really depends on what aspect of your code you are trying to unit test. From what I can see, this test simply confirms that you are storing the Policy objects as expected. You might write further tests for any logic that depends on the Policy instances themselves.

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

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
Here is the scenario. I'm writing my geo-ruby oracle adapter for Ruby On Rails
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here is the problem that I am trying to solve. I have two folders
Here is a code snippet I was working with: int *a; int p =
Here is the problem I'm trying to solve for my game. I have this
Here I am trying to change the value of the following check box while
Here is my scenario. I have a website running under AppPool1 and that works
Here the scenario is I have many .aspx pages if any page gives error,

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.