本文介绍了如何测试的ModelState?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何测试 Controller.ViewData.ModelState ?我想preFER做它没有任何模拟框架。

How can I test Controller.ViewData.ModelState? I would prefer to do it without any mock framework.

推荐答案

您不必如果您使用的,当然你的数据存储库模式,使用模拟。

You don't have to use a Mock if you're using the Repository Pattern for your data, of course.

一些例子:
http://www.singingeels.com/Articles/Test_Driven_Development_with_ASPNET_MVC.aspx

// Test for required "FirstName".
   controller.ViewData.ModelState.Clear();

   newCustomer = new Customer
   {
       FirstName = "",
       LastName = "Smith",
       Zip = "34275",    
   };

   controller.Create(newCustomer);

   // Make sure that our validation found the error!
   Assert.IsTrue(controller.ViewData.ModelState.Count == 1, 
                 "FirstName must be required.");

这篇关于如何测试的ModelState?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 14:12