如何对返回 PartialViewResult 的 MVC Action 进行单元测试?
我有一个 Action
如下:
public PartialViewResult MyActionIWantToTest(string someParameter)
{
// ... A bunch of logic
return PartialView("ViewName", viewModel);
}
当我检查结果时,它有一些属性,但它们要么为 null,要么为空。 唯一具有任何内容的属性是 ViewEngineCollection,它不包含任何特定于我的方法的内容。
有人有一些测试 PartialViewResult
的示例代码吗?
I have an Action
as follows:
public PartialViewResult MyActionIWantToTest(string someParameter)
{
// ... A bunch of logic
return PartialView("ViewName", viewModel);
}
When I inspect the result, It has a few properties, but they are either null, or empty.
The only property that has anything is the ViewEngineCollection
which doesn't contain anything specific to my method.
Does anyone have some example code that tests a PartialViewResult
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有一个看起来像这样的
Action
:注意:
MyPartialViewModel
是一个简单的类,只有一个属性 -SomeValue
。NUnit 示例可能如下所示:
Say you have an
Action
that looks something like this:Note:
MyPartialViewModel
is a simple class with only one property -SomeValue
.An NUnit example may look like this:
接受的答案对我不起作用。我执行了以下操作来解决我所看到的测试失败问题。
这就是我的行动:
我必须给结果一个类型才能使其工作。我将 PartialViewResult 用于返回部分视图的操作,而不是返回完整视图并使用“查看结果”的其他操作。这是我的测试方法:
The accepted answer did not work for me. I did the following to resolve the test failure I was seeing.
This was my action:
I had to give the result a type in order for it to work. I used PartialViewResult for my action returning a Partial View, as opposed to my other actions that return a full view and use View Result. This is my test method: