如何使用犀牛制品嘲弄的HttpContext.Application [英] How to use Rhino Mocks to Mock an HttpContext.Application
问题描述
我是新来嘲弄框架和使用RhinoMocks的协助我的MVC应用程序的单元测试已经开始。
I'm new to Mocking frameworks and have started using RhinoMocks to assist with my MVC App Unit Testing.
我使用斯科特Hanselmanns MVC模拟帮助,以协助嘲讽的HttpContext。
我已经成功地(一段时间后)嘲笑一些我需要什么,但是来了脱胶,当它涉及到的HttpContext的应用程序属性。
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"];
这得到我的MVC应用程序上的Application_Start创建。
This gets created on Application_Start in my MVC App.
已更新下面先回答(额外code为清楚起见)
目前在测试设置我做的:
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);
这调用抛出一个异常NullReference,Application对象。
This call throws a NullReference Exception, for the Application object.
我的问题是双重的:
1)这是正确的做法或有我做错了从一个设计/体系结构的角度来看?
1) Is this the right approach or have I done something wrong from a design / architecture perspective?
2)为什么不这项工作?!
2) Why doesn't this work?!
谢谢,在前进。
推荐答案
在没有研究得太深,这看起来大多是正确的。
Without delving too deeply, this looks mostly correct.
应用程序属性是HttpContextBase虚拟的,所以你应该能够从犀牛设置一个返回值是 - 假设你嘲讽HttpContextBase斯科特Hanselmanns后一样。
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:
- 您是否设置了退货
controllerToTest.ControllerContext? - 您是否设置了一回的是的
对象的HttpContext属性? - 您是否设置了一回的是的
对象应用程序属性?
我想问的原因是,通常当你期望的设置,你已经有了将被调用作为测试的一部分对象的引用,所以像你与你的<$ C你不会做一个产业链$ C> controllerToTest.ControllerContext.HttpContext。
期待()电话。
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.
编辑:
我想我看到这个问题,我认为这是与此部分:
I think I see the problem, and I think it's with this part:
期待(CTX =&GT; ctx.Application [Globals.GlobalsKey])
我认为你假定索引器的工作方式相同的属性,当他们没有。你真正需要做的是建立你的 APPSTATE
对象的期望接收呼叫的Item属性,如:
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:
// setup expectations -- assumes some of the expectations and mocks
// the from original question
mockHttpBase.Expect(ctx => ctx.Application).Return(appState);
appState.Expect(ctx => ctx.Item(Globals.GlobalsKey)).Return(tmpAppGlobals);
// run the test
这篇关于如何使用犀牛制品嘲弄的HttpContext.Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!