在mvc 2中测试自定义actionresult

时间:2011-06-16 04:54:13

标签: c# asp.net-mvc unit-testing

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

我做了一些环顾四周,我看不出一个简单的方法来测试响应是否正确。我认为最好的方法是测试实例化自定义动作结果&模型,调用execute方法,然后有些人检查controllercontexts的响应。唯一的问题是,如何将响应输出作为字符串?我在某处读到你不能将响应输出流作为文本?如果是这样,我如何验证我的内容是否被写入响应流?我可以测试我的序列化程序以降低风险,但是想知道如何覆盖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);
        }

    } 

1 个答案:

答案 0 :(得分:8)

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

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

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());