且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单元测试控制器

更新时间:2023-02-17 07:48:33

在你的单元测试,你应该模拟你的模型有一个错误,因为这是你想要测试(错误路径)是什么。在测试的模型是有效的,这就是为什么它重定向你的谢谢的看法。为了模拟你可以在行为部分之前做,在你的单元测试错误:

Inside your unit test you should simulate that your model has an error because it's what you want to test (the error path). In your test the model is valid that's why it redirects you to the "Thanks" view. To simulate the error you can do that in your unit test before "act" section :

UsersController.ModelState.AddModelError("username", "Bad username"); 

就以这个例子来看看:http://www.thepursuitofquality.com/post/53/how-to-test-modelstateisvalid-in-aspnet-mvc.html

更多关于AddModelError方法在这里:http://msdn.microsoft.com/en-us/library/system.web.mvc.modelstatedictionary.addmodelerror.aspx

More about AddModelError method here : http://msdn.microsoft.com/en-us/library/system.web.mvc.modelstatedictionary.addmodelerror.aspx