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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With