There are quite a few things that you are creating and verifying here.
1. That the wiring is working.
Here you can either use an 'integration test' and run the ASP
or expect the event to be registered and fire them to see that the wiring works (See
Events)
2. That the event method is working.
In this case you can either fire the event, or call the event directly but you will have to mock the HttpApplication and HttpContext to return the desired results. Using Chained Natural Mocks is the way to go here.
example
suppose your code looks like this:
public class SecurityHttpModule : IHttpModule
{
public void Dispose() {}
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(application_BeginRequest);
}
void application_BeginRequest(object source, EventArgs e)
{
HttpApplication application = (HttpApplication) source;
HttpContext context = application.Context;
// get the user, populate the Thread.CurrentUser…
HttpCookie cookie = context.Request.Cookies.Get("CookieName");
if (!cookie.Secure)
{
// do something...
}
}
}
Here are some tests
[Test]
[VerifyMocks]
public void TestThatEventsAreWiredUpCorrectly()
{
SecurityHttpModule underTest = new SecurityHttpModule();
HttpApplication application = new HttpApplication();
// Record Mocks - make sure that the event is registered
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
application.BeginRequest += null;
}
underTest.Init(application);
// this is verified using the VerifyMocks decorator
}
[Test]
[VerifyMocks]
public void FireEventsWithCookieSet()
{
SecurityHttpModule underTest = new SecurityHttpModule();
HttpApplication application = new HttpApplication();
MockedEvent beginRequestHandle;
// Record Mocks
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
// mock the event registration
application.BeginRequest += null;
beginRequestHandle = RecorderManager.LastMockedEvent;
// mock secure cookie
HttpCookie mockedCookie = application.Context.Request.Cookies.Get("CookieName");
recorder.CheckArguments(); // make sure that we request the correct cookie
recorder.ExpectAndReturn(mockedCookie.Secure, true);
}
underTest.Init(application);
// fire a new request
beginRequestHandle.Fire(application, EventArgs.Empty);
// test that is behaves normally
}