我正在使用 ASP.NET MVC 和 C# 开发一个 Web 应用程序。我正在使用 NUnit 和 Rhino Mock 为这个 web 应用程序创建一个单元测试。我的问题是我的控制器的操作方法中有一个 Response 对象,当我执行单元测试时,我的测试失败,因为 Response 对象是空引用。
我是否需要在操作中分离此 Response 对象调用,还是有更好的方法来解决此问题?
public ActionResult Login( string user, string password )
{
Response.Cookies[ "cookie" ].Value = "ck";
...
return View();
}
Run Code Online (Sandbox Code Playgroud)
请指教。
非常感谢。
小智 7
控制器真正缺少的是它的 HttpContext。如果需要,应在测试方法中显式添加:
[Test]
public void TestMethod()
{
// Assume the controller is created once for all tests in a setup method
_controller.ControllerContext.HttpContext = new DefaultHttpContext();
var result = _controller.Login("username", "verySaf3Passw0rd");
// Asserts here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4268 次 |
| 最近记录: |