Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test for a method that takes a HttpResponse object as a parameter. OutputStream is not available

I am trying to create a unit test for a method that takes a HttpResponse object as a parameter. What the correct way of doing this? I hope you seasoned unit testers out there can help me.

Additional information: I tried creating a fake HttpResponse object by passing in a StringWriter.

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HttpResponse response = new HttpResponse(sw);
RssGenerator.Generate(response, otherParameters);

The test fails with the message: System.Web.HttpException: OutputStream is not available when a custom TextWriter is used. The method being tested is part of a class library dll. It uses the Response object's OutputStream to create an RSSFeed with an XMLWriter.

like image 886
James Lawruk Avatar asked Aug 31 '09 13:08

James Lawruk


People also ask

Which of the following is correct about a unit test case?

Q 10 - Which of the following is correct about a Unit Test Case? A - A Unit Test Case is a part of code which ensures that the another part of code (method) works as expected.

How do you write a unit test case?

For unit testing, I found both Test Driven (tests first, code second) and code first, test second to be extremely useful. Instead of writing code, then writing test. Write code then look at what you THINK the code should be doing. Think about all the intended uses of it and then write a test for each.


1 Answers

Need to use namespace System.Web.Abstractions

In particular, take a HttpResponseBase (http://msdn.microsoft.com/en-us/library/system.web.httpresponsebase.aspx) as an input parameter instead of HttpResponse. Then you can mock the HttpResponseBase from your tests. When you have a real HttpResponse to pass in, use HttpResponseWrapper to produce a HttpResponseBase.

like image 85
Frank Schwieterman Avatar answered Oct 27 '22 02:10

Frank Schwieterman