出现404错误

时间:2011-11-30 11:02:26

标签: asp.net-mvc-3 unit-testing nunit moq http-status-code-404

我有以下404错误ActionResult,当找不到网页时抛出:

public ActionResult InvokeHttp404(HttpContextBase httpContext) {
      IController errorController = new ErrorController();
      var errorRoute = new RouteData();
      errorRoute.Values.Add("controller", "Error");
      errorRoute.Values.Add("action", "Http404");
      errorRoute.Values.Add("url", httpContext.Request.Url.OriginalString);
      errorController.Execute(new RequestContext(httpContext, errorRoute));
      return new EmptyResult();
    }

我正试图通过以下测试对其进行单元测试:

[TestMethod]
        public void Details_Get_404Handler()
        {
            // Arrange
            var controller = GetController(new Repository(), FakeHttpContext());

            // Act
            var result = controller.Details(3442399) as ViewResult; // invalid Id (not found)

            //Assert
            Assert.AreEqual("NotFound", result.ViewName);
        }

我很长时间以来一直坚持这个测试,它会在请求Url.OriginalString的代码行上抛出一个null异常。阅读后,我发现上一篇文章Mocking HttpContextBase with Moq,发现第二个答案非常有用,如下所示:(我添加了一行处理Url字符串的行)

public static HttpContextBase FakeHttpContext()
        {
            var context = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();
            var response = new Mock<HttpResponseBase>();
            var session = new Mock<HttpSessionStateBase>();
            var server = new Mock<HttpServerUtilityBase>();
            var user = new Mock<IPrincipal>();
            var identity = new Mock<IIdentity>();

            request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/a", UriKind.Absolute));
            request.Setup(req => req.ApplicationPath).Returns("~/");
            request.Setup(req => req.AppRelativeCurrentExecutionFilePath).Returns("~/");
            request.Setup(req => req.PathInfo).Returns(string.Empty);
            response.Setup(res => res.ApplyAppPathModifier(It.IsAny<string>()))
                .Returns((string virtualPath) => virtualPath);
            user.Setup(usr => usr.Identity).Returns(identity.Object);
            identity.SetupGet(ident => ident.IsAuthenticated).Returns(true);

            context.Setup(ctx => ctx.Request).Returns(request.Object);
            context.Setup(ctx => ctx.Response).Returns(response.Object);
            context.Setup(ctx => ctx.Session).Returns(session.Object);
            context.Setup(ctx => ctx.Server).Returns(server.Object);
            context.Setup(ctx => ctx.User).Returns(user.Object);

            return context.Object;
        }

所以我终于打败了请求Url.OriginalString的代码行,这要归功于帖子和答案。但我现在紧跟在它之后的代码行: errorController.Execute(new RequestContext(httpContext, errorRoute));。现在测试失败,并在该行上出现空引用错误。因为我已经伪造了httpContext,所以我对这里的null感到有些困惑。有人可以帮我这个吗?

编辑:

更改了ValueProviderFactoriesExtensions:

public static class ValueProviderFactoresExtensions
{
    public static ValueProviderFactoryCollection ReplaceNameValueCollectionWith<TOriginal>(this ValueProviderFactoryCollection factories, Func<ControllerContext, NameValueCollection> sourceAccessor)
    {
        var original = factories.FirstOrDefault(x => typeof(TOriginal) == x.GetType());
        if (original != null)
        {
            var index = factories.IndexOf(original);
            factories[index] = new NameValueCollectionProviderFactory(sourceAccessor);
        }
        return factories;
    }

    public static ValueProviderFactoryCollection ReplaceHttpFileCollectionWith<TOriginal>(this ValueProviderFactoryCollection factories, Func<ControllerContext, HttpFileCollectionBase> sourceAccessor)
    {
        var original = factories.FirstOrDefault(x => typeof(TOriginal) == x.GetType());
        if (original != null)
        {
            var index = factories.IndexOf(original);
            factories[index] = new HttpFileCollectionProviderFactory(sourceAccessor);
        }
        return factories;
    }

    class NameValueCollectionProviderFactory : ValueProviderFactory
    {
        private readonly Func<ControllerContext, NameValueCollection> sourceAccessor;


        public NameValueCollectionProviderFactory(Func<ControllerContext, NameValueCollection> sourceAccessor)
        {
            this.sourceAccessor = sourceAccessor;
        }


        public override IValueProvider GetValueProvider(ControllerContext controllerContext)
        {
            return new NameValueCollectionValueProvider(sourceAccessor(controllerContext), CultureInfo.CurrentCulture);
        }

    }
    class HttpFileCollectionProviderFactory : ValueProviderFactory
    {
        private readonly Func<ControllerContext, HttpFileCollectionBase> sourceAccessor;


        public HttpFileCollectionProviderFactory(Func<ControllerContext, HttpFileCollectionBase> sourceAccessor)
        {
            this.sourceAccessor = sourceAccessor;
        }


        public override IValueProvider GetValueProvider(ControllerContext controllerContext)
        {
            return new HttpFileCollectionValueProvider(controllerContext);
        }

    }
}

为响应Marnix的答案而改变了代码:

public static HttpContextBase FakeHttpContext()
        {
            var context = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();
            var response = new Mock<HttpResponseBase>();
            var session = new Mock<HttpSessionStateBase>();
            var server = new Mock<HttpServerUtilityBase>();
            var user = new Mock<IPrincipal>();
            var identity = new Mock<IIdentity>();
            var files = new Mock<HttpFileCollectionBase>();



            request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/a", UriKind.Absolute));
            request.Setup(req => req.ApplicationPath).Returns("~/");
            request.Setup(req => req.AppRelativeCurrentExecutionFilePath).Returns("~/");
            request.Setup(req => req.PathInfo).Returns(string.Empty);
            request.Setup(req => req.ContentType).Returns("text/html");
            request.Setup(req => req.QueryString).Returns(new NameValueCollection());
            request.Setup(req => req.Form).Returns(new NameValueCollection());
            request.Setup(req => req.Files).Returns(files.Object);
            response.Setup(res => res.ApplyAppPathModifier(It.IsAny<string>()))
                .Returns((string virtualPath) => virtualPath);
            user.Setup(usr => usr.Identity).Returns(identity.Object);
            identity.SetupGet(ident => ident.IsAuthenticated).Returns(true);
            context.Setup(ctx => ctx.Request).Returns(request.Object);
            context.Setup(ctx => ctx.Response).Returns(response.Object);
            context.Setup(ctx => ctx.Session).Returns(session.Object);
            context.Setup(ctx => ctx.Server).Returns(server.Object);
            context.Setup(ctx => ctx.User).Returns(user.Object);

            ValueProviderFactories.Factories
                .ReplaceNameValueCollectionWith<FormValueProviderFactory>(ctx => ctx.HttpContext.Request.Form)
                .ReplaceNameValueCollectionWith<QueryStringValueProviderFactory>(ctx => ctx.HttpContext.Request.QueryString)
                .ReplaceHttpFileCollectionWith<HttpFileCollectionValueProviderFactory>(ctx => ctx.HttpContext.Request.Files);

            return context.Object;
        }

仍然无法使其工作,仍然在同一行代码上抛出空引用。不知道堆栈跟踪是否在努力寻找视图?堆栈跟踪:

System.Web.Mvc.ViewResult.FindView(ControllerContext context)

1 个答案:

答案 0 :(得分:2)

模拟控制器的上下文有点棘手。在MVC3中还有一个额外的步骤来阻止值提供者触及HttpContext.Current。

This answer解释了您需要做的事情。

更新:您可能不会直接使用ValueProviderFactories,但控制器上的ActionInvoker会这样做。在值提供者的默认实现的深处某处访问HttpContext.Current,它不在Web请求之外设置,在单元测试期间导致NullReferenceException。您可以通过替换默认值提供程序来防止这种情况(如链接答案中所述)。

将上述链接中的ValueProviderFactoresExtensions课程复制到您的测试项目中,将以下代码添加到您的FakeHttpContext方法中:

var form = new NameValueCollection();
var queryString = new NameValueCollection();

request.Setup( x => x.Form ).Returns( form );
request.Setup( x => x.QueryString ).Returns( queryString );

ValueProviderFactories.Factories
.ReplaceWith<FormValueProviderFactory>(ctx => form)
.ReplaceWith<QueryStringValueProviderFactory>(ctx => queryString);