Skip to content

Instantly share code, notes, and snippets.

@johnmmoss
Created February 15, 2015 12:52
Show Gist options
  • Save johnmmoss/f69beea8c4911f4f1d5f to your computer and use it in GitHub Desktop.
Save johnmmoss/f69beea8c4911f4f1d5f to your computer and use it in GitHub Desktop.
Rhino Mock Example MVC Asserts
public void ExampleMvcAsserts()
{
// Setup an error in the model so that ModelState.IsValid returns false.
// Used to check that the ModelState.IsValid check is in place.
controller.ViewData.ModelState.AddModelError("Surname", "The Surname field is required.");
// Call the controller method. This usually returns an ActionResult, which may need to be cast.
var result = _userController.Create(user).Result;
// Assert that the redirect result is of a particular type
Assert.That(result, Is.TypeOf<RedirectToRouteResult>());
// Assert that the controller redirects to the correct view
Assert.AreEqual("Index", ((RedirectToRouteResult)result).RouteValues["action"]);
// Check manaully added model state errors
Assert.IsTrue(controller.ModelState.Keys.Contains("Email"));
Assert.AreEqual("The Email address is already in use.", controller.ModelState["Email"].Errors[0].ErrorMessage);
// Check the model class is returned correctly in the view.
var userModel = viewResult.Model as User;
Assert.AreEqual(userModel.Forename, "John");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment