asp.net MVC ModelState在我的单元测试中为null。为什么?
问题描述:
ModelState在我的单元测试中总是返回null。我希望有人能告诉我为什么。asp.net MVC ModelState在我的单元测试中为null。为什么?
鉴于以下控制器:
public class TestController : Controller
{
public ViewResult Index()
{
return View();
}
}
我的测试得到空值的ModelState与这个测试:
public void ModelState_Is_Not_Null()
{
TestController controller = new TestController();
var result = controller.Index();
// This test is failing:
Assert.IsNotNull(controller.ViewData.ModelState);
}
如果我改变控制器返回一个新的ViewResult()我不明白null:
public class TestController : Controller
{
public ViewResult Index()
{
return new ViewResult();
}
}
但是... IsValid()返回true时,它不应该如果我这样做:
public class TestController : Controller
{
public ViewResult Index()
{
ModelState.AddModelError("Test", "This is an error");
return new ViewResult();
// I don't get null in the test for ModelState anymore, but IsValid()
// returns true when it shouldn't
}
}
我想我在这里做了一些根本性的错误,我不知道是什么。任何人都可以将我指向正确的方向吗?
答
感谢您检查Darin。
我安装了MVC 1 RC和MVC 2 RC 2版本。我卸载了他们两个,安装了MVC 1,现在一切都按预期行事。测试不会失败。
+0
有趣。我有与ASP.NET MVC 4相同的问题 – SiberianGuy 2012-10-07 06:44:34
我刚刚运行了你的第一个测试(你说它失败了),它运行得很好。所以也许还有别的。 – 2010-03-05 08:02:24