I need to force TryUpdateModel return false in unit testing in asp.net mvc web application, but it always return true.
my controller action is:
public JsonResult SaveStep(string path, string title, string definitionOfDone, int limit, bool isNewStep)
{
Step step = new Step();
if (TryUpdateModel(step)){
// code
}
}
Simply add an error to the model state in the arrange phase of your unit test:
Now when you invoke the
_controllerUnderTest.SaveStepin the act phase of your unit test the ModelState.IsValid will return false.