My ModelState remains valid in Controller test even when it's not.http://forums.asp.net/t/1529916.aspx/1?My+ModelState+remains+valid+in+Controller+test+even+when+it+s+not+Tue, 15 Jun 2010 00:56:15 -040015299163700914http://forums.asp.net/p/1529916/3700914.aspx/1?My+ModelState+remains+valid+in+Controller+test+even+when+it+s+not+My ModelState remains valid in Controller test even when it's not. <p>Hi everyone,</p> <p>I wrote a test that asserts that ModelState.IsValid is false but the test keeps failing. On closer examination, I find that ModelState.IfValid is always returning true for my controller tests. I had used Data annotations to mark up the model with required attributes.</p> <p><br> </p> <p>Am I missing anything?</p> 2010-02-25T16:15:49-05:003701158http://forums.asp.net/p/1529916/3701158.aspx/1?Re+My+ModelState+remains+valid+in+Controller+test+even+when+it+s+not+Re: My ModelState remains valid in Controller test even when it's not. <p>What are you doing to invalidate your model state? Why do you expect it to return false?<br> </p> 2010-02-25T18:25:49-05:003701187http://forums.asp.net/p/1529916/3701187.aspx/1?Re+My+ModelState+remains+valid+in+Controller+test+even+when+it+s+not+Re: My ModelState remains valid in Controller test even when it's not. <p>For instance, I would set up a model and exclude a&nbsp;required parameter. Then, i would pass that new model onto the&nbsp;new Controller.Action(model) method. </p> <p>&nbsp;</p> <p>my action method signature uses&nbsp;</p> <pre class="prettyprint">public ActionResult ActionMethod(Model model){ if(ModelState.IsValid){ // Do the right thing return RedirectToAction(&quot;Index&quot;); } return View(); }</pre> <p><br> &nbsp;</p> 2010-02-25T18:46:07-05:003701204http://forums.asp.net/p/1529916/3701204.aspx/1?Re+My+ModelState+remains+valid+in+Controller+test+even+when+it+s+not+Re: My ModelState remains valid in Controller test even when it's not. <p>Simply executing a method during a unit test does just that - executes a method, and no more. &nbsp;The MVC pipeline doesn't run, so binding and validation don't run.</p> <p>If you want to unit test your action's behavior for when ModelState is invalid, you need to manually make ModelState invalid. &nbsp;You can use <i>controller.ModelState.AddModelError(&quot;&quot;, &quot;dummy error message&quot;)</i> from within your unit test to force ModelState to be invalid.</p> 2010-02-25T18:57:55-05:003701308http://forums.asp.net/p/1529916/3701308.aspx/1?Re+My+ModelState+remains+valid+in+Controller+test+even+when+it+s+not+Re: My ModelState remains valid in Controller test even when it's not. <p>Many thanks.</p> <p>P&nbsp;</p> 2010-02-25T19:54:29-05:003926636http://forums.asp.net/p/1529916/3926636.aspx/1?Re+My+ModelState+remains+valid+in+Controller+test+even+when+it+s+not+Re: My ModelState remains valid in Controller test even when it's not. <p>Did you solved the problem?</p> <p>What&nbsp; is your framework version ? 3.5 ? 4&nbsp; ?</p> <p>I had the same problem. I updated from framework 3.5 to 4 and works like a charm</p> 2010-06-15T00:56:15-04:00