I have some code inside a controller called AdminController.
public ActionResult Login()
{
AdminModel model = new AdminModel();
return View(model);
}
When I inspect the view in a test method…
var result = controller.Login();
//assert
Assert.IsInstanceOfType(result, typeof(ViewResult));
Assert.AreEqual("Login", ((ViewResult)result).ViewName);
It doesn’t show a view name. However the Login view is definitely returned. I’m wondering why no view name is set? I thought it defaulted to the action result method name?
The ViewName is only set if you set the
viewNameparameter when your return the view. For example:Otherwise, if no view name is set it uses the default view.
Therefore, to test for the default view your assert should be written: