I am starting to learn Unit testing in MVC4.
This is my controller.
public class AccountController : Controller
{
public ActionResult Register(User user)
{
if (ModelState.IsValid)
{
return View("RegistrationSuccessful");
}
return View("Register");
}
}
And this is the test.
public class AccountControllerTests
{
[TestMethod]
public void invalid_registration_details_should_show_registration_form_again()
{
var controller = new AccountController();
var user = new User();
user.Name = null;
var result = controller.Register(user) as ViewResult;
Assert.AreEqual("Register", result.ViewName);
}
}
And this is the model.
public class User
{
[Required]
public string Name { get; set; }
}
When I call controller.Register(user) I think the model binder does not come in to picture as I am instantiating the controller myself and not through the framework. So I think ModelState.IsValid will be true by default.
How do I test this? How can model validation be triggered in unit tests?