0

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?

JadedEric
  • 1,943
  • 2
  • 26
  • 49
  • Are you using MVC with ASP.NET 3.5 or is this a Web Forms Application? – Rami A. Dec 17 '11 at 20:24
  • I'm using 3.5 with a normal web forms. I'm not 100% sold on going 100% MVC and would like the tread on the idea by building something "conventional" in a non-conventional manner. feel i'd actually learn something :) – JadedEric Dec 26 '11 at 18:56
  • I'm not sure I understand what you are trying to accomplish. Code.Authenticate(), , AuthenticateSimulate, and customer are undefined. I recommend simplifying your problem. Are you submitting an http form as part of your test? Are you really just trying to test a method in your data access layer that validates the username and password of a user? If you want a method to behave differently depending on a condition, add a parameter to the method. I'm not sure why you want to start building something in a non-convential manner. Your time is better spent learning the standards first. – Rami A. Dec 27 '11 at 09:52
  • the idea here is to build a single delegate test that accepts an anonymous condition similar to how Action(Func()) works. if it makes more sense. – JadedEric Jan 04 '12 at 07:33
  • Why does HttpSimulator not work with your mocked EF classes? – Rami A. Jan 04 '12 at 08:20

1 Answers1

1

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

Rami A.
  • 10,302
  • 4
  • 44
  • 87