testing a custom actionresult in mvc 2
i have an actionresult that overrides Execute (as you do) and the actionresult basically receives a model, serializes it, and writes it out to the reponse via response.write(model as text).
ive done some looking around and i cant see an easy way to test the response is correct. i think the best way is to test instantiate the custom actionresult & model, call the execute method, and then somehome inspect the controllercontexts' response. Only problem is, how do i get the reponses output as a string? i read somewhere that you cant get the responses outputstream as text? if so, how do i verify that my content is being written to the response stream? i can test m开发者_开发问答y serialiser to reduce the risk, but id like to get some coverage over the ExecuteResult override...
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);
}
}
You can get the output as text, but you do have to do a bit of extra work to make it all testable.
You'll need MVCContrib.TestHelper to get mocks set up for all the MVC components. On top of that I have a bit of code setting up access to the relevant parts of the request:
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;
}
}
}
Note that OutputStream is now a MemoryStream rather than an actual web connection. With that set up you just need a few more lines to get the output that would be sent to the client:
Result.ExecuteResult(c.ControllerContext);
var ms = (MemoryStream)c.HttpContext.Response.OutputStream;
c.HttpContext.Response.Output.Flush();
ResultHtml = new UTF8Encoding().GetString(ms.GetBuffer());
精彩评论