result.viewname 始终为 string.empty
我似乎无法返回 result.ViewName 用于 Nunit 测试,因为它总是返回 string.empty。我已在控制器内明确设置了视图的名称,并希望测试能够识别该名称。我四处寻找,似乎如果我明确设置它,我应该找回视图名称。有人有什么想法吗?
public class HomeController : Controller
{
public ActionResult Index()
{
return View("Index");
}
}
我的测试看起来像这样
[Test]
public void TestIndexView()
{
var controller = new HomeController();
var result = controller.Index() as ViewResult;
Assert.AreEqual("Index", result.ViewName);
}
I cannot seem to return the result.ViewName for use in Nunit tests as it always returns string.empty. I have explicitly set the name of the view inside my controller and would expect the test to pick this up. I have had a hunt around and it seems that I should get the Viewname back if I set it explicitly. Any one got any ideas?
public class HomeController : Controller
{
public ActionResult Index()
{
return View("Index");
}
}
My test looks like this
[Test]
public void TestIndexView()
{
var controller = new HomeController();
var result = controller.Index() as ViewResult;
Assert.AreEqual("Index", result.ViewName);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过清理和重建解决方案?它应该可以正常工作。
Did you try cleaning and rebuilding solution? It should work without problems.
您需要
return new View("Index");
。如果这是 C 语言,原因是您在Index()
中创建View
的方式,它只是存储在堆栈上并超出范围(并且因此在函数结束时被收集)。这会导致 C 崩溃,但 C# 在这方面似乎更聪明一些。You need to
return new View("Index");
. If this were C the reason why is because of the way you're creating theView
inIndex()
it is just stored on the stack and goes out of scope (and thus gets collected) when the function ends. This would cause C to crash, but C# appears to be a little smarter in this regard.