views:

52

answers:

2

I have a third party framework that I'm using to write out an excel document and have the user download it.

The problem is that the user is attempting to export a large number of records, and it's throwing an OutOfMemoryException.

I'm trying to write a test to verify that this is happening in the third party framework and not in my code, but the Save method takes an HttpResponse object as one of the methods.

How can I get/mock/whatever an HttpResponse object to pass?

Example use:

excel.Save("test.xls", 
    OpenType.OpenInExcel, 
    FileType.Excel2003,
    HttpContext.Current.Response);

Obviously the HttpContext.Current.Reponse doesn't work in a unit test, hence my problem.

+1  A: 

[Disclaimer: I work at Typemock]

You don't need to have an actual HttpResponse object instead use Isolator to create a fake instance of type HttpResponse and return it when HttpContext.Current.Response is called:

var fake = Isolate.Fake.Instance<HttpResponse>();
Isolate.WhenCalled(() => HttpContext.Current.Response).WillReturn(fake);

You can set properties on the fake object and change it's behaviour using Isolate.WhenCalled.

The merit of this approach is that you do not need to set a complicated environment, in fact you can use this code inside a simple unit test.

Dror Helper
+2  A: 

The HttpRequestBase from .Net 3.5 SP1 is designed for this purpose, but it won't do you any good unless the third-party code uses it. (it's a drop-in replacement so it's not hard to use)

If the other code doesn't use it, you can try hosting the code in Cassini and requesting it over HTTP.

SLaks