且构网

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

为什么这个asp.net mvc的单元测试失败?

更新时间:2023-02-17 13:16:17

您不应该再用控制器来处理多个请求,这是你在做什么,在这里。

You should not reuse a controller to handle multiple requests, which is exactly what you are doing here.

无论如何,如果你查看​​源$ C ​​$ C为MVC 你会发现这种现象的原因:

Anyway, if you check the source code for MVC you'll find the reason for this behavior:

protected internal virtual ViewResult View(string viewName, string masterName, object model)
{
    if (model != null)
    {
        base.ViewData.Model = model;
    }
    return new ViewResult { ViewName = viewName, MasterName = masterName, ViewData = base.ViewData, TempData = base.TempData };
}

如果模型为空,这不是分配给ViewData.Model属性。
如果你想正确的行为,为你的第二个电话一个新的控制器,以 HomeController.Strangeness

If the model is null, it's not assigned to the ViewData.Model property. If you want the correct behaviour, create a new controller for your second call to HomeController.Strangeness.