问题描述:

I want to write some UT for my code

which uses HttpContext.Current

How can I fake this object?

网友答案:

Check out the WatiN library and tools. You can fake a IE thread.

网友答案:

I am using Phil Haack HttpSimulator for that purpose :

http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx

网友答案:

You could use a commercial tool like Typemock Isolator to fake the HttpContext directly, without wrapping it in your own code:

Isolate.WhenCalled(()=> controller.HttpContext.Current.Session["UserID"])
    .WillReturn("YourID");
网友答案:

If you own that code, don't use HttpContext directly. Use some interface that abstract HttpContext away. Then replace the implementation of that interface in unit tests.

相关阅读:
Top