MVC单元测试,访问发送到View的模型

时间:2022-09-25 11:40:42

I have the following method in one of my Views,

我的一个观点中有以下方法,

public ActionResult EditRoute(int? id)
{
    // Do Work
    return View(new RoutingFormViewModel(obj1, obj2, obj3));
}

What I'd like to do is get the RoutingFormViewModel that is being passed to the view in my unit test, is this possible?

我想要做的是获取在我的单元测试中传递给视图的RoutingFormViewModel,这可能吗?

I've tried the following but don't seem to be getting anywhere:

我尝试了以下但似乎没有得到任何地方:

ActionResult result = con.EditRoute(null);
ViewResult v = (ViewResult)result;

I'm basically looking how I can access the View's model from my test. Any help appreciated.

我基本上在寻找如何从我的测试中访问View的模型。任何帮助赞赏。

1 个解决方案

#1


Sure, just access the v.ViewData.Model property. Your Model will be there. But first check that your action worked as expected and the result is actually a ViewResult. I don't know if you have any other paths in the action code that could end in a different result.

当然,只需访问v.ViewData.Model属性。你的模型将在那里。但首先检查您的操作是否按预期工作,结果实际上是ViewResult。我不知道你在动作代码中是否有任何其他路径可能以不同的结果结束。

#1


Sure, just access the v.ViewData.Model property. Your Model will be there. But first check that your action worked as expected and the result is actually a ViewResult. I don't know if you have any other paths in the action code that could end in a different result.

当然,只需访问v.ViewData.Model属性。你的模型将在那里。但首先检查您的操作是否按预期工作,结果实际上是ViewResult。我不知道你在动作代码中是否有任何其他路径可能以不同的结果结束。