1
我有一个单元测试,测试的路由段数太多,但我想扩展它来测试Response Status Code
(应该返回404代码)。单元测试MVC4响应状态代码
是否有可能使用模拟的httpContext并检查状态码?
HttpContextMock.Object.Response.StatusCode
返回0(由于嘲笑我想的HttpContext
的一部分)。
[TestMethod]
public void RouteWithTooManySegments()
{
var routes = new RouteCollection();
MyApp.RouteConfig.RegisterRoutes(routes);
var httpContextMock = new Mock<HttpContextBase>();
var httpResponseMock = new Mock<HttpResponseBase>();
httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath)
.Returns("~/dawd/dawdaw/dawdaw/daw");
RouteData routeData = routes.GetRouteData(httpContextMock.Object);
Assert.IsNull(routeData);
}
使用ErrorController
返回 “404” 的观点和适当Response Status Code
:
public class ErrorController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult NotFound()
{
Response.StatusCode = 404;
return View();
}
}
和Web.Config
规则
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
</httpErrors>
是的我正在考虑更多的Web集成测试的方向。 – netdis