I've got a very simple test method, using MvcContrib.TestBuilder to setup the controller (and mock the HttpContext etc...)
[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));
}
My implementation looks as simple as:
public ActionResult Http404()
{
Response.StatusCode = 404;
return View();
}
However, my test is always failing, because Response.StatusCode
is always 0
Even if I debug, and evaluate Response.StatusCode
after Response.StatusCode = 404;
it is still 0
The HttpResponse instance is a mock. So you should assert it like this:
instead of:
But instead of setting some status codes in my controller action I would simply return the proper
ActionResult
:and test it like this:
Looks more readable IMHO.