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!
You should mock the
userManager.GetUsersmethod 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 theuserManager.GetUsersmethod this method needs to be virtual:For example:
Now in your unit test you could provide a mock instance of the
IUsersManagerinterface and define expectations for theGetUsersmethod.Using a mocking framework such as Rhino Mocks this is a trivial task:
and using MVCContrib.TestHelper it provides you more fluent syntax simplifies the mocking of standard HTTP artifacts such as the context, session, cookies, …: