Cucumber 场景中使用 Rspec/Mocha 进行存根
我使用 Cucumber 作为带有 rspec/mocha 模拟的 BDD 框架。理想情况下,我们不会在黄瓜规范中模拟/存根行为;然而这里的情况很特殊。在这里让您简要了解问题;我有两个功能:产品功能和购物车功能。
购物车功能当前正在模拟从 3 方系统获取的一些产品。在黄瓜运行中,购物车功能先于产品功能运行。产品功能部分场景采用真实通话。理想情况下,我们希望一旦场景完成,像 Product.stub(:find) 这样的类级别存根将被清除;但事实并非如此。类级别存根会持续到下一个 Cucumber 场景运行并干扰调用。
我交替使用 mocha 和 rspec 来诱导存根;但无济于事;在任何一种情况下,存根都会保留并阻止我的产品功能在购物车功能之后运行。两者单独运行都很好。
有没有人遇到过类似的黄瓜存根未重置的问题?有什么办法可以恢复原来的班级行为吗?
任何帮助将不胜感激。
干杯
I am using Cucumber as the BDD framework with rspec/mocha mocking. Ideally we would not mock/stub behavior in cucumber specs; however the scenario is exceptional here. To give you the brief idea of problem here; I have two features product feature and cart feature.
Cart feature is currently mocking some of the product fetch from 3 party system. And in cucumber run cart feature runs before product feature. Product feature uses real call for some scenarios. Ideally we would expect that Class level stubs like Product.stub(:find) would be cleared once the scenarios are completed; however that is not the case. Class level stubs linger on to the next cucumber scenario run and interfere with the call.
I interchangeably used mocha and rspec to induce the stubs; but no avail; in either case the stub remains and prevents my product feature to run after cart feature. Both of them run fine in isolation.
Has anyone faced similar issue of stubs not being reset in cucumber? Is there a way, I can restore the original class behavior?
Any help will be appreciated.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 mocha 集成,您应该将以下内容添加到 features/support 文件夹中的文件中(如 mocha.rb)。
after 块应该释放所有模拟。
来源:http://gist.github.com/80554
For mocha integration, you should add the following to a file in your features/support folder (like mocha.rb)
The after block should release all mocks.
Source: http://gist.github.com/80554
mocha 1.1 的语法略有不同:
Syntax is slightly different for mocha 1.1: