开发者

How do I test a third party framework when I need to pass in an HttpResponse

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

The pro开发者_开发技巧blem 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.


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.


[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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜