I'm new to Mocking frameworks and have started using RhinoMocks to assist with my MVC App Unit Testing.
I'm using Scott Hanselmanns MVC Mock Helper to assist in mocking the HttpContext. I've succesfully (after some time) mocked some of what I need but have come unstuck when it comes to the Application property of the HttpContext.
In my application I store an object in the Application and retrieve it within a Controller like:
SomeObj foo = (SomeObj)Application["fooKey"];
This gets created on Application_Start in my MVC App.
UPDATED FOLLOWING FIRST ANSWER (additional code for clarity) Currently in the test setup I do:
HttpContextBase mockHttpBase = mocks.FakeHttpContext();
controllerToTest = new SomeController();
mocks.SetFakeControllerContext(controllerToTest);
HttpApplicationStateBase appState =
MockRepository.GenerateStub<HttpApplicationStateBase>();
Globals tmpAppGlobals =
new Globals();
mockHttpBase.Expect(ctx => ctx.Application).Return(appState);
mockHttpBase.Expect(ctx => ctx.Application[Globals.GlobalsKey]).
Return(tmpAppGlobals);
In my unit test setup I do:
Globals tmpAppGlobals = new Globals();
controllerToTest.ControllerContext.HttpContext.
Expect(ctx => ctx.Application[Globals.GlobalsKey]).
Return(tmpAppGlobals);
This call throws a NullReference Exception, for the Application object.
My question is two fold:
1) Is this the right approach or have I done something wrong from a design / architecture perspective?
2) Why doesn't this work?!
Thanks, in advance.
Without delving too deeply, this looks mostly correct.
The Application property is virtual on HttpContextBase, so you should be able to set up a return value for it from Rhino -- Assuming you're mocking HttpContextBase as Scott Hanselmanns post does.
Some possible causes, which are really just guesses from lack of information:
The reason I ask is that typically when you do expectation setups, you already have references to the objects that will be called as part of your test, so you wouldn't do a property chain like you do with your
controllerToTest.ControllerContext.HttpContext. Expect()
call.Edit:
I think I see the problem, and I think it's with this part:
Expect(ctx => ctx.Application[Globals.GlobalsKey])
I think you're assuming that indexers work the same as properties, when they don't. What you really need to do is set up an expectation on your
appState
object to receive a call to the Item property, like this:you could use the below for Moq. It took me awhile how to mock the HttpApplication, and the appState.Object is the return method duh!