Asp.net mvc asp.net MVC ModelState在我的单元测试中为空。为什么?

Asp.net mvc asp.net MVC ModelState在我的单元测试中为空。为什么?,asp.net-mvc,null,unit-testing,modelstate,Asp.net Mvc,Null,Unit Testing,Modelstate,在我的单元测试中,ModelState总是返回null。我希望有人能告诉我为什么 给定以下控制器: public class TestController : Controller { public ViewResult Index() { return View(); } } 此测试的ModelState的My测试为null: public void ModelState_Is_Not_Null() { TestController contro

在我的单元测试中,ModelState总是返回null。我希望有人能告诉我为什么

给定以下控制器:

public class TestController : Controller
{
   public ViewResult Index()
    {
        return View();
    }
}
此测试的ModelState的My测试为null:

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时返回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
    }
}

我想我在这里做了一些根本错误的事情,我不知道是什么。谁能给我指一下正确的方向吗?

谢谢你的检查,达林


我安装了MVC1RC和MVC2RC版本。我卸载了它们,安装了MVC 1,现在一切正常。测试没有失败。

我刚刚运行了您的第一个测试(您说它失败的测试),它运行得非常好。也许还有别的事,很有趣。我对ASP.NETMVC4也有同样的问题