我有一个非常简单的测试方法,使用 MvcContrib.TestBuilder 来设置控制器(并模拟 HttpContext 等......)
[Test]
public void http404_returns_status_code_of_404()
{
var builder = new TestControllerBuilder();
controller = new ErrorController();
builder.InitializeController(controller);
var result = controller.Http404();
Assert.That(controller.Response.StatusCode, Is.EqualTo(404));
}
我的实现看起来很简单:
public ActionResult Http404()
{
Response.StatusCode = 404;
return View();
}
但是,我的测试总是失败,因为Response.StatusCode
总是 0
即使我调试,然后评估Response.StatusCode
它Response.StatusCode = 404;
仍然是 0