在mvc 2中测试自定义操作结果

本文关键字:自定义 操作 结果 测试 mvc | 更新日期: 2023-09-27 17:59:11

我有一个覆盖Execute的actionresult(正如您所做的那样),actionresult基本上接收一个模型,序列化它,并通过response.write(模型为文本)将其写入响应。

我环顾四周,看不出一个简单的方法来测试答案是否正确。我认为最好的方法是测试实例化自定义actionresult&模型,调用execute方法,然后somehome检查控制器上下文的响应。唯一的问题是,如何将reponses输出作为字符串?我在某个地方读到,你无法将响应输出流作为文本?如果是,我该如何验证我的内容是否被写入响应流?我可以测试我的serializer来降低风险,但我想覆盖ExecuteResult覆盖。。。

public class CustomResult: ActionResult
    {
        private readonly object _contentModel;
        private readonly ContentType _defaultContentType;
        public CustomResult(object contentModel, ContentType defaultContentType)
        {
            _contentModel = contentModel;
            _defaultContentType = defaultContentType;
        }
        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.Write(serialized model);
        }
    } 

在mvc 2中测试自定义操作结果

您可以将输出作为文本,但您必须做一些额外的工作才能使其全部可测试。

您需要MVCContrib.TestHelper来为所有MVC组件设置mock。最重要的是,我有一些代码设置访问请求的相关部分:

public class CustomTestControllerBuilder : TestControllerBuilder
{
    public CustomTestControllerBuilder()
    {
        var httpContext = new Moq.Mock<HttpContextBase>();
        var request = new Moq.Mock<HttpRequestBase>();
        var response = new Moq.Mock<HttpResponseBase>();
        var server = new Moq.Mock<HttpServerUtilityBase>();
        var _cache = HttpRuntime.Cache;
        httpContext.Setup(x=> x.Request).Returns(request.Object);
        httpContext.Setup(x => x.Response).Returns(response.Object);
        httpContext.Setup(x => x.Session).Returns(Session);
        httpContext.Setup(x => x.Server).Returns(server.Object);
        httpContext.Setup(x => x.Cache).Returns(_cache);

        var items = new Dictionary<object, object>();
        httpContext.Setup(x => x.Items).Returns(items);

        QueryString = new NameValueCollection();
        request.Setup(x => x.QueryString).Returns(QueryString);
        Form = new NameValueCollection();
        request.Setup(x => x.Form).Returns(Form);
        request.Setup(x => x.AcceptTypes).Returns((Func<string[]>)(() => AcceptTypes));
        var files = new WriteableHttpFileCollection();
        Files = files;
        request.Setup(x => x.Files).Returns(files);
        Func<NameValueCollection> paramsFunc = () => new NameValueCollection { QueryString, Form };
        request.Setup(x => x.Params).Returns(paramsFunc);
        request.Setup(x => x.AppRelativeCurrentExecutionFilePath).Returns(
                (Func<string>)(() => AppRelativeCurrentExecutionFilePath));
        request.Setup(x => x.ApplicationPath).Returns((Func<string>)(() => ApplicationPath));
        request.Setup(x => x.PathInfo).Returns((Func<string>)(() => PathInfo));
        request.Setup(x => x.RawUrl).Returns((Func<string>)(() => RawUrl));
        response.SetupProperty(x => x.Status);
        httpContext.SetupProperty(x=>x.User);
        var ms = new MemoryStream(65536);
        var sw = new StreamWriter(ms);
        response.SetupGet(x=>x.Output).Returns(sw);
        response.SetupGet(x => x.OutputStream).Returns(ms);
        response.Setup(x => x.Write(It.IsAny<string>())).Callback((string s) => { sw.Write(s); });
        response.Setup(x => x.Write(It.IsAny<char>())).Callback((char s) => { sw.Write(s); });
        response.Setup(x => x.Write(It.IsAny<object>())).Callback((object s) => { sw.Write(s); });
        //_mocks.Replay(HttpContext);
        //_mocks.Replay(request);
        //_mocks.Replay(response);
        TempDataDictionary = new TempDataDictionary();

        HttpContext = httpContext.Object;
    }
}
}

请注意,OutputStream现在是MemoryStream,而不是实际的web连接。设置好后,您只需要再多几行就可以获得将发送到客户端的输出:

 Result.ExecuteResult(c.ControllerContext);
 var ms = (MemoryStream)c.HttpContext.Response.OutputStream;
 c.HttpContext.Response.Output.Flush();
 ResultHtml = new UTF8Encoding().GetString(ms.GetBuffer());