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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:10:22+00:00 2026-05-23T14:10:22+00:00

I have the following code in a UserController within my ASP.NET MVC application: public

  • 0

I have the following code in a “UserController” within my ASP.NET MVC application:

public class UserController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("List");
    }

    public ActionResult List()
    {
        IUserRepository repo = new UserRepository();
        IQueryable<Business.Entities.User> users = repo.GetAll();
        return View("List", users);
    }
}

Using Moq, I would like to mock the database call “repo.GetAll()”. Here is what I have for my test:

[Test]
public void List()
{
    Mock<IUserRepository> mockRepo = new Mock<IUserRepository>();
    mockRepo.Setup(ur => ur.GetAll()).Returns(MockedGetAll());
    var v = mockRepo.Object.GetAll();

    var controller = new UserController();
    var result = controller.List() as ViewResult;
    var model = result.ViewData.Model as IQueryable<User>;

    Assert.AreEqual("List", result.ViewName);
    Assert.IsNotNull(model);
    Assert.Greater(model.Count(), 0);
}

I also have a function that would return some static data to satisfy the test:

private IQueryable<User> MockedGetAll()
{
    List<User> users = new List<User>();
    users.Add(new User(1, "mark.paterson", "mark.paterson@yahoo.com", "Mark", "Paterson", false, true));
    users.Add(new User(2, "nikki.paterson", "nikki.paterson@yahoo.com", "Nikki", "Paterson", false, true));
    return users.AsQueryable();
}

The test breaks at “Assert.Greater”. I get a 0 records instead of 2. When I debugged the code, the code actually returns the result of the database call, which is supposed to be 0 records, instead of the mocked data.

  • 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-05-23T14:10:23+00:00Added an answer on May 23, 2026 at 2:10 pm

    The following line is killing everything and introducing an impossible to unit test in isolation strong coupling between your controller and data access layer:

    IUserRepository repo = new UserRepository();
    

    Absolutely never write anything like this in any application (not only ASP.NET MVC). No matter what you do if you write code like this it will always break in your unit test and you will not be able to test it.

    This is impossible to mock/unit test.

    You should use constructor injection to weaken coupling between your layers:

    public class UserController : Controller
    {
        private readonly IUserRepository _repo;
        public UserController(IUserRepository repo)
        {
            _repo = repo;
        }
    
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }
    
        public ActionResult List()
        {
            IQueryable<Business.Entities.User> users = _repo.GetAll();
            return View("List", users);
        }
    }
    

    Now you can unit test in isolation by mocking:

    [Test]
    public void List()
    {
        Mock<IUserRepository> mockRepo = new Mock<IUserRepository>();
        mockRepo.Setup(ur => ur.GetAll()).Returns(MockedGetAll());
        var v = mockRepo.Object.GetAll();
    
        var controller = new UserController(mockRepo.Object);
        var result = controller.List() as ViewResult;
        var model = result.ViewData.Model as IQueryable<User>;
    
        Assert.AreEqual("List", result.ViewName);
        Assert.IsNotNull(model);
        Assert.Greater(model.Count(), 0);
    }
    

    Obviously because now your controller depends on this repository you could use a DI framework to configure your dependencies.

    People that don’t want to use DI framework often write code like this and provide 2 constructors (one for unit testing and one for the real application):

    private readonly IUserRepository _repo;
    public UserController(IUserRepository repo)
    {
        _repo = repo;
    }
    
    public UserController(): this(new UserRepository())
    {
    
    }
    

    I am providing this in order to illustrate another thing that you should absolutely never do and to stress on the fact that this is poor man’s DI.

    Haacked also discussed about those issues in his blog post.

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

Sidebar

Related Questions

I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code in my controller called UserController : public ActionResult Details(string
I have following code: public abstract class Operand<T> { public T Value { get;
I have following code public partial class MainWindow : Window { public MainWindow() {
I have following code for 3 DataGridView Controls in my VB.NET winform application. How
I have following code: public class reader extends Activity { WebView mWebView; String mFilename;
I have following code I want to test: public class MessageService { private MessageDAO
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a

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.