简体   繁体   English

我怎么能模拟TryValidateModel

[英]How can i mock TryValidateModel

I need to Mock TryValidateModel, Because now if I run the unit test TryValidateModel return me a null. 我需要模拟TryValidateModel,因为现在如果我运行单元测试TryValidateModel返回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. 我知道ModelState.IsValid是检查我们是否有正确模型的好方法,但我不能在这里使用它。

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... 你必须模拟你正在测试的控制器的ControllerContext ...

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. 您不需要测试执行验证的ASP.Net代码,它已经有测试。 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 您可以执行集成测试,在其中实例化整个控制器并将模型传递给它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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