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 my 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:
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: