Cucumber 场景中使用 Rspec/Mocha 进行存根

发布于 2024-09-08 13:53:23 字数 440 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淤浪 2024-09-15 13:53:23

对于 mocha 集成,您应该将以下内容添加到 features/support 文件夹中的文件中(如 mocha.rb)。

require "mocha"

World(Mocha::Standalone)

Before do
  mocha_setup
end

After do
  begin
    mocha_verify
  ensure
    mocha_teardown
  end
end

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)

require "mocha"

World(Mocha::Standalone)

Before do
  mocha_setup
end

After do
  begin
    mocha_verify
  ensure
    mocha_teardown
  end
end

The after block should release all mocks.

Source: http://gist.github.com/80554

我要还你自由 2024-09-15 13:53:23

mocha 1.1 的语法略有不同:

require 'mocha/api'

World(Mocha::API)

Before do
  mocha_setup
end

After do
  begin
    mocha_verify
  ensure
    mocha_teardown
  end
end

Syntax is slightly different for mocha 1.1:

require 'mocha/api'

World(Mocha::API)

Before do
  mocha_setup
end

After do
  begin
    mocha_verify
  ensure
    mocha_teardown
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文