【发布时间】:2010-09-26 08:03:08
【问题描述】:
我一直在阅读有关单元测试控制器逻辑的正确方法的各种教程。采取以下措施:
public ActionResult Login()
{
//Return the index view if we're still here
return View();
}
街上的说法是建立一个类似这样的测试方法:
[TestMethod]
public void TestLoginView()
{
//Set up an instance of the controller
var thisController = new UserController();
//Invoke the index action
var actionResult = (ViewResult)thisController.Login();
//Validate the test
Assert.AreEqual("Login", actionResult.ViewName);
}
断言按预期工作。然而,这个控制器有一个基类,它覆盖 OnActionExecuting 函数,以设置各种页面元素 chrome(导航元素、面包屑等)。这一点逻辑永远不会受到影响。
我可以轻松地测试控制器中使用的模型,但是我希望在控制器层进行测试。想法?
【问题讨论】: