sobota, 15 sierpnia 2015

Testing MVC Controllers - invalid models

As you might seen at the previous post, there was part of my Controller I didn't test. What happens when model will be invalid? I have started with uncommenting two tests that I've had already prepared for this scenario.
Then I've added some DataAnnotations to make sure I can create instance of invalid Employee (could create nullable properties describing limbs, but thats not the point here).
As you can see in the tests, there was special field for my invalid, _invalidEntity so I eagerly assigned to it my poor Employee with terribly short family name of "Ni". Excited to have my test complete I quickly did (Ctrl + R, N) which is default in Visual Studio to run test that haven't been yet performed and...they failed.
After debugging it turned out that model won't be validated during the test so ModelState.IsValid will be always true. It turned out that testing this scenario is way easier than I expected. Everything that I've had to do was to add validation error manually before acting. Now I didn't need _invalidEntity anymore, instead of that small method was added to be called in the begining of both tests.
That way I've covered entire Create and Edit methods. After quick check on constructor references I've noticed that I use just one of them so I've got rid of the other two and ran the test. Result shown 94.12% coverage, this time everthing except for disposal method. I like to see how numbers grow so "I'll be back!".

Brak komentarzy:

Prześlij komentarz