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