将 T4MVC 与 MvcContrib.TestHelper AssertViewRendered 结合使用时如何测试默认视图
我在 ASP.NET MVC 3 项目中使用 T4MVC。我有以下基本测试:
[TestMethod]
public void IndexReturnsIndexView()
{
var controller = new HomeController();
var result = controller.Index();
result.AssertViewRendered().ForView(MVC.Home.Views.Index);
}
如果控制器方法返回默认视图,则测试失败:
public virtual ActionResult Index()
{
return View();
}
给出的错误是:
MvcContrib.TestHelper.ActionResultAssertionException: Expected view name '~/Views/Home/Index.cshtml', actual was ''
但如果我重写 View
来指定哪个 viewName 返回:
public virtual ActionResult Index()
{
return View(MVC.Home.Views.Index);
}
我尝试使用以下断言,但仍然不走运:
result.AssertViewRendered().ForView(MVC.Home.Index().GetT4MVCResult().Action);
引发以下错误:
MvcContrib.TestHelper.ActionResultAssertionException: Expected view name 'Index', actual was ''
然后我意识到我误读了断言失败,因此我将测试更改为:
result.AssertViewRendered().ForView(String.Empty);
测试通过,但测试本身似乎变得毫无用处。
最好我不想按名称指定所有视图,那么我该如何测试呢?为了澄清一下,我正在使用 MvcContrib.Mvc3.TestHelper-ci 3.0.96.0 ,我今天从 NuGet 安装了它。
更新
这不是问题的答案,但我已经开始执行以下操作,这作为测试用例提供了更多价值:
using (var controller = new FeatureController(mockGateway))
{
// Act
var result = controller.Index();
var model = result.ViewData.Model as MyModel;
// Assert
Assert.IsNotNull(model, "Model is null or wrong type");
result.AssertViewRendered().WithViewData<MyModel>();
// Alternative Assert for model data
Assert.IsTrue(model.Items.Count > 0);
}
I am using T4MVC in my ASP.NET MVC 3 project. I have the following basic test:
[TestMethod]
public void IndexReturnsIndexView()
{
var controller = new HomeController();
var result = controller.Index();
result.AssertViewRendered().ForView(MVC.Home.Views.Index);
}
The test fails if the controller method returns the default View:
public virtual ActionResult Index()
{
return View();
}
The error given is:
MvcContrib.TestHelper.ActionResultAssertionException: Expected view name '~/Views/Home/Index.cshtml', actual was ''
But the test passes if I override the View
to specify which viewName
to return:
public virtual ActionResult Index()
{
return View(MVC.Home.Views.Index);
}
I tried using the following assertion, but still not luck:
result.AssertViewRendered().ForView(MVC.Home.Index().GetT4MVCResult().Action);
The following error is raised:
MvcContrib.TestHelper.ActionResultAssertionException: Expected view name 'Index', actual was ''
I then realized that I had misread the assertion failure, so I changed the test to this:
result.AssertViewRendered().ForView(String.Empty);
The test passes, but the test itself seems to be useless.
Preferably I don't want to have to specify all views by name, so how do I test this? To clarify, I am using the MvcContrib.Mvc3.TestHelper-ci 3.0.96.0, which I installed today from NuGet.
UPDATE
This isn't an answer to the question, but I have started doing the following instead, which provides more value as a test case:
using (var controller = new FeatureController(mockGateway))
{
// Act
var result = controller.Index();
var model = result.ViewData.Model as MyModel;
// Assert
Assert.IsNotNull(model, "Model is null or wrong type");
result.AssertViewRendered().WithViewData<MyModel>();
// Alternative Assert for model data
Assert.IsTrue(model.Items.Count > 0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将这个问题保留了相当长的一段时间,以便其他人可以回答,如果他们愿意的话。现在我自己来回答这个问题。
以下检查以确保返回的视图包含预期的模型,以及更合适的模型中的数据。对于相关控制器来说,这是一个更好的单元测试。
I left this open for a considerable amount of time, so that anyone else could answer if they chose to. I'll now answer this myself.
The following checks to make sure that the view returned contains the expected model, and more appropriately data within that model. This is a much better unit test for the controller in question.
AssertViewRendered().ForView() 针对您显式传递给 View() 的视图名称进行测试,这是您的控制器操作。如果像您所做的那样,您没有在操作中指定视图名称,那么 .ForView() 将针对空字符串进行测试,如您所见。
如果你调用 View("Index");在您的操作中,您可以在测试中调用 .ForView("Index") 。
我认为如果您的操作可以返回不同的可能视图,那么这个断言将是最有用的。
AssertViewRendered().ForView() tests against the view name that you explicitly pass to View() is your controller action. If, as you're doing, you don't specify a view name in your action, then .ForView() will be testing against an empty string, as you've seen.
If you called View("Index"); in your action, you could call .ForView("Index") in your test.
I would think that this assertion would be most useful if your action could return different possible views.