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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:07:53+00:00 2026-06-04T03:07:53+00:00

I am new to MVC .NET (I have previously worked on Ruby On Rails).

  • 0

I am new to MVC .NET (I have previously worked on Ruby On Rails).

I was wondering how I could write a unit test that would check that the correct arguments are passed into the view.

public ActionResult Users()
{
  var users = userManager.GetUsers();
  return View(users);
}

How do I test that the View has been passed with the list of users? Do I simply mock the View static method or is there a better approach?

Thanks!

  • 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-04T03:07:54+00:00Added an answer on June 4, 2026 at 3:07 am

    You should mock the userManager.GetUsers method and then assert that the controller action returned a ViewResult with model that equals to the mocked list of users. Of course in order to be able to mock the userManager.GetUsers method this method needs to be virtual:

    For example:

    public class HomeController: Controller
    {
        private readonly IUsersManager _usersManager;
        public HomeController(IUsersManager usersManager)
        {
            _usersManager = usersManager;
        }
    
        public ActionResult Users()
        {
            var users = _usersManager.GetUsers();
            return View(users);
        }    
    }
    

    Now in your unit test you could provide a mock instance of the IUsersManager interface and define expectations for the GetUsers method.

    Using a mocking framework such as Rhino Mocks this is a trivial task:

    [TestMethod]
    public void Users_Action_Should_Query_The_UserManager_Repository_And_Pass_The_Result_To_The_View()
    {
        // arrange
        var expectedUsers = new User[] { new User() };
        var usersManagerStub = MockRepository.GenerateStub<IUsersManager>();
        usersManagerStub.Stub(x => x.GetUsers()).Return(expectedUsers);
        var sut = new HomeController(usersManagerStub);
    
        // act
        var actual = sut.Users();
    
        // assert
        Assert.IsInstanceOfType(actual, typeof(ViewResult));
        var viewResult = actual as ViewResult;
        Assert.AreEqual(expectedUsers, viewResult.Model);
    }
    

    and using MVCContrib.TestHelper it provides you more fluent syntax simplifies the mocking of standard HTTP artifacts such as the context, session, cookies, …:

    [TestMethod]
    public void Users_Action_Should_Query_The_UserManager_Repository_And_Pass_The_Result_To_The_View()
    {
        // arrange
        var expectedUsers = new User[] { new User() };
        var usersManagerStub = MockRepository.GenerateStub<IUsersManager>();
        usersManagerStub.Stub(x => x.GetUsers()).Return(expectedUsers);
        var sut = new HomeController(usersManagerStub);
    
        // act
        var actual = sut.Users();
    
        // assert
        actual
            .AssertViewRendered()
            .WithViewData<User[]>()
            .ShouldEqual(expectedUsers, "");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to ASP.NET MVC so this could have an obvious answer. Right now
I have a new solution that is an ASP.NET MVC 2.0 application. I have
I have created a new ActionFilter for an ASP.NET MVC application that I'm creating.
I have an application (ASP.NET MVC) that uses a Next New method to get
I have a new asp.net mvc project and i am trying to figure out
I have created the following project structure for my new asp.net mvc project any
We recently added a new TeamCity build agent. Because it doesn't have ASP.NET MVC
i'm new in the asp.net mvc development and have a quastin about the standart
I am new to ASP.NET MVC 3 and I want to have username/id as
I'm new in ASP.NET MVC, I have many actions in my controllers, so they

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.