Question

I'm thinking, while writing some unit tests for an application to build intuitive methods into my classes that renders differently depending on which "class" called the method.

So, I'm in an NUnit class, and need to test whether a FormsAuthenticationTicket was successfully created for the authenticated user:

Assert.DoesNotThrow(typeof(Application.AuthenticationFailure), 
   delegate { Code.Authenticate(); }

Problem I'm facing is HttpContext is not valid in unit tests. Found several solutions but like what Phil Haack did with his HttpSimulator. This does not work directly within my unit test code, as I'm mocking EF classes (not recommended I know), and the simulator affects the connection strings.

It is a better option to rather move the data calls out of the unit tests, but I'm learning about anonymous methods in the process and would like to do something like:

delegate<Test> { AuthenticateSimulate(customer); }

Is there a nice alternative in 3.5 to do something like this?

Was it helpful?

Solution

Maybe try a mock object framework like RhinoMocks, Moq, or TypeMock Isolator for example, or an isolation framework like Microsoft Moles.

http://research.microsoft.com/en-us/projects/moles/
"Replace any .NET method with your own delegate!"
"Moles is a lightweight framework for test stubs and detours in .NET that is based on delegates. Moles may be used to detour any .NET method, including non-virtual/static methods in sealed types."

Edit
Also see:
http://stephenwalther.com/blog/archive/2008/07/01/asp-net-mvc-tip-12-faking-the-controller-context.aspx

With Mvc, for example:
http://mvccontrib.codeplex.com/wikipage?title=TestHelper&referringTitle=Documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top