单元测试遗留代码 - 重构什么以适应 Rhino Mocks 上下文?
我正在尝试通过单元测试追溯地适应一些遗留代码。我对单元测试没有太多经验,这让我很不高兴:)
有问题的代码是一个 MVC 控制器。我重构了它以删除直接依赖项,并将它们替换为界面,这些界面都很好且直接。然而,为了进行测试,我很难让所有上下文模拟都正确。
许多代码都会调用 Request.QueryString 和 Request.Params。我不完全确定为什么它将两者分开处理,但我讨厌摆弄任何我实际上不需要改变的东西。
因此,我将其放在一起来初始化模拟上下文:
var controller = new TestController();
var request = MockRepository.GenerateMock<HttpRequestBase>();
var context = MockRepository.GenerateMock<HttpContextBase>();
var collection = new System.Collections.Specialized.NameValueCollection();
collection.Add("Id", "1");
context.Expect( c => c.Request ).Return( request ).Repeat.Any();
request.Expect( r => r.Params ).Return( collection ).Repeat.Any();
request.Expect( x => x.QueryString["foo"] ).Return("bar").Repeat.Any();
controller.ControllerContext = new ControllerContext(context, new RouteData(), controller);
“TestController”只是一个继承自正在测试的控制器的本地类。上面的代码编译得非常愉快,但是当我尝试运行它时,我收到以下错误:
Previous method 'HttpRequestBase.get_QueryString();' requires a return value or an exception to throw.
谷歌搜索这似乎并没有多大帮助。
问题是,我应该在这里重构什么:我的测试以便尝试构建它,或者我的基本代码以便它只使用 Params 或 QueryString 但不能同时使用两者?
干杯, 马特
I'm trying to retroactively fit some legacy code with unit tests. Not being terribly experienced with unit tests, this is not making me happy :)
The code in question is an MVC controller. I re-factored it to remove direct dependencies and replaced them with interfaces which was all nice and straight forward. However I'm having real trouble getting all the context mocks correct in order to do the testing.
A lot of the code makes calls to both Request.QueryString and Request.Params. I'm not entirely sure why it treats the two separately, but I'm loathe to fiddle with anything I don't actually need to change.
So I put this together to initialise a mocked context:
var controller = new TestController();
var request = MockRepository.GenerateMock<HttpRequestBase>();
var context = MockRepository.GenerateMock<HttpContextBase>();
var collection = new System.Collections.Specialized.NameValueCollection();
collection.Add("Id", "1");
context.Expect( c => c.Request ).Return( request ).Repeat.Any();
request.Expect( r => r.Params ).Return( collection ).Repeat.Any();
request.Expect( x => x.QueryString["foo"] ).Return("bar").Repeat.Any();
controller.ControllerContext = new ControllerContext(context, new RouteData(), controller);
"TestController" is just a local class that inherits from the controller that's being tested. The above code compiles quite happily but when I try and run it I get the following error:
Previous method 'HttpRequestBase.get_QueryString();' requires a return value or an exception to throw.
Googling this doesn't seem to be terribly helpful.
Question is, what do I refactor here: my test in order to try and get it to build, or my base code so that it just uses Params or QueryString but not both?
Cheers,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不能直接回答您的问题,但我认为您应该尝试使用 MVC Contrib 的 TestControllerBuilder 用于此类测试。它处理控制器的许多依赖性,让您专注于需要测试的部分。
This doesn't answer your question directly, but I think that you should try using MVC Contrib's TestControllerBuilder for this kind of testing. It deals with a lot of the dependencies of a controller and lets you concentrate on the bits you need to test.