问题描述
我在写一个单元测试,我叫这样的操作方法
I'm writing a unit test and I call an action method like this
var result = controller.Action(123);
结果是的ActionResult
,我需要以某种方式获取模式,任何人知道如何做到这一点?
result is ActionResult
and I need to get the model somehow, anybody knows how to do this?
推荐答案
在我的版本的ASP.NET MVC的有控制器上没有动作
方法。但是,如果你的意思是查看
方法,在这里是你如何通过单元测试结果中包含了正确的模式。
In my version of ASP.NET MVC there is no Action
method on Controller. However, if you meant the View
method, here's how you can unit test that the result contains the correct model.
首先,如果你只从一个特定的动作返回的ViewResult,申报方法returning的ViewResult代替的ActionResult 。
First of all, if you only return ViewResult from a particular Action, declare the method as returning ViewResult instead of ActionResult.
作为一个例子,考虑这个指数的行动
As an example, consider this Index action
public ViewResult Index()
{
return this.View(this.userViewModelService.GetUsers());
}
你可以得到的模型很容易,因为这
you can get to the model as easily as this
var result = sut.Index().ViewData.Model;
如果您的方法签名的返回类型代替的ActionResult的ViewResult,你需要转换它首先的ViewResult。
If your method signature's return type is ActionResult instead of ViewResult, you will need to cast it to ViewResult first.
这篇关于如何从一个ActionResult模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!