J
JahMic
I recently took on the unenviable task of using the HttpContext object
in my unit tests, as any body who has tried this before knows it is
not so easy. The three main approaches I have found are: 1) Making a
mock object, 2) Faking a Context via SimpleWorkerRequest, and 3)
Creating your own host environment ala CreateApplicationHost.
I didn't like the first approach, because their might be some
variation between a mock object and the real thing. That reduces a lot
of the power of unit testing. (...thoughts?) The Second approach is
fairly well publicized over the web but seems to only good for .NET
1.x. Trying some of the projects unchanged in a 2.0 environment
simply does not work, as the WorkerRequest seems to want to work out
of an actual host environment.
The third approach is promising but I'm still having some problems
getting the HttpContext into the unit tests. A good article on this
approach:
http://hyperthink.net/blog/CommentView,guid,271632d2-07e3-41af-9e58-9a7e25348b8c.aspx
In it, he exposes the HttpContext by a call to:
System.Runtime.Remoting.Messaging.CallContext.SetData("HttpContext",
request).
Pretty cool, except when the execution returns back at the unit test
call, HttpContext.Current is still null. Thoughts?
To either return the HttpContext in a call or expose it as a public
field causes NUnit to throw an exception that it is not
serializable... Serializable??? I don't particularly if it's
serialized or not... Marking the class as [Serializable] does not
help and I'm now at a loss on how to get through this. Thoughts?
Any help, much appreciated.
J
in my unit tests, as any body who has tried this before knows it is
not so easy. The three main approaches I have found are: 1) Making a
mock object, 2) Faking a Context via SimpleWorkerRequest, and 3)
Creating your own host environment ala CreateApplicationHost.
I didn't like the first approach, because their might be some
variation between a mock object and the real thing. That reduces a lot
of the power of unit testing. (...thoughts?) The Second approach is
fairly well publicized over the web but seems to only good for .NET
1.x. Trying some of the projects unchanged in a 2.0 environment
simply does not work, as the WorkerRequest seems to want to work out
of an actual host environment.
The third approach is promising but I'm still having some problems
getting the HttpContext into the unit tests. A good article on this
approach:
http://hyperthink.net/blog/CommentView,guid,271632d2-07e3-41af-9e58-9a7e25348b8c.aspx
In it, he exposes the HttpContext by a call to:
System.Runtime.Remoting.Messaging.CallContext.SetData("HttpContext",
request).
Pretty cool, except when the execution returns back at the unit test
call, HttpContext.Current is still null. Thoughts?
To either return the HttpContext in a call or expose it as a public
field causes NUnit to throw an exception that it is not
serializable... Serializable??? I don't particularly if it's
serialized or not... Marking the class as [Serializable] does not
help and I'm now at a loss on how to get through this. Thoughts?
Any help, much appreciated.
J