Faking HttpContext for unit testing

G

Guest

Hi all

I've read the thread titled: ""faking" an HTTPContext when running unit tests
http://communities2.microsoft.com/c...spnet&mid=a884a918-b04a-4912-b2ea-e691dc5230f

However, the solutions provided don't work for me

I have a class like this

public class MyClas

public static string Foo(string s

return HttpContext.Current.Server.HtmlEncode(s)



When I call MyClass.Foo() from within an executing request, it works. But when I call MyClass.Foo() from within an NUnit test, it fails because there is no HttpContext

Does anyone know how I can write a test for MyClass.Foo()

Like I said, I've tried the solutions from that other related thread, but none of them work

Thanks
Steven
 
B

bruce barker

you cannot inherit from HttpContext, so you need to write a wrapper around
it that you use instead. this wrapper and handle the null case.

-- bruce (sqlwork.com)


element533 said:
Hi all,

I've read the thread titled: ""faking" an HTTPContext when running unit tests"
http://communities2.microsoft.com/communities/newsgroups/en-us/default.aspx?
dg=microsoft.public.dotnet.framework.aspnet&mid=a884a918-b04a-4912-b2ea-e691
dc5230f5

However, the solutions provided don't work for me.

I have a class like this:

public class MyClass
{
public static string Foo(string s)
{
return HttpContext.Current.Server.HtmlEncode(s);
}
}


When I call MyClass.Foo() from within an executing request, it works. But
when I call MyClass.Foo() from within an NUnit test, it fails because there
is no HttpContext.
 
G

Guest

I would like to find a way to make it work with the framework's HttpContext, rather than having to write my own implementation that mimics it

----- bruce barker wrote: ----

you cannot inherit from HttpContext, so you need to write a wrapper aroun
it that you use instead. this wrapper and handle the null case

-- bruce (sqlwork.com


element533 said:
Hi all tests

http://communities2.microsoft.com/communities/newsgroups/en-us/default.aspx
dg=microsoft.public.dotnet.framework.aspnet&mid=a884a918-b04a-4912-b2ea-e69
dc5230f

public static string Foo(string s

return HttpContext.Current.Server.HtmlEncode(s)
when I call MyClass.Foo() from within an NUnit test, it fails because ther
is no HttpContext
 
G

Guest

I had the same problem in NUnit when using HttpContext to access Cache. But it worked when I used HttpRuntime (although I'm not sure if it's a good approach on this issue :D).

Kiko
 
G

Guest

I had the same problem using HttpContext to access Cache in NUnit. But using HttpRuntime worked for me (although I'm not sure if this is a good approach :D).

Kiko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top