简体   繁体   中英

How can i mock TryValidateModel

I need to Mock TryValidateModel, Because now if I run the unit test TryValidateModel return me a null. with a lot of exceptions.

I know ModelState.IsValid is a good way to check if we have the correct model but I can't use it here.

if (TryValidateModel(object))

I would like manage the return of this method.

You would have to mock the ControllerContext of the controller you are testing...

This is however in violation of what a unit test should normally do, what are you actually trying to test here? You don't need to test the ASP.Net code which performs the validation, it already has tests. All you would achieve is asserting that your model has the specific attributes in place, which isn't really behaviour and doesn't need tested as such.

All your custom logic should be outside the controller, in some domain class, which you would test instead...

You can do an integration test where you instance the whole controller and pass the model to it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM