模拟和存根可以在 Cucumber 步骤之间持续存在吗?

发布于 2024-12-27 01:36:05 字数 672 浏览 1 评论 0原文

我有一个依赖于名为 PSC 的第三方 API 的应用程序,但我想将我的 Cucumber 测试与对 PSC 的 API 调用隔离开来。

因此,我编写了几个 Cucumber 步骤:

When /^we pretend that PSC is up$/ do
  PscV1.default_psc_connection("test user").stub!(:default_connection_is_up?).and_return(true)
end

When /^we pretend like PSC assignments exist for all subjects$/ do
  PscV1.default_psc_connection("test user").stub!(:assignment_exists?).and_return(true)
end

...这些存根应该做的是让 Cucumber 场景认为 API 调用正在工作。但是,存根似乎不会在步骤之间持续存在,因此我的场景中的后续步骤不会获得存根返回值,它们会尝试进行实际的 API 调用,因此会失败。

有没有办法让存根至少持续整个场景?我已经在其他 Cucumber 测试中成功使用了存根,因此我知道它们通常可以工作,但这是我第一次编写 Cucumber 步骤,其全部目的是提供存根。

I have an app that relies on a 3rd party API called PSC, but I want to isolate my cucumber tests from API calls to PSC.

So, I wrote a couple of cucumber steps:

When /^we pretend that PSC is up$/ do
  PscV1.default_psc_connection("test user").stub!(:default_connection_is_up?).and_return(true)
end

When /^we pretend like PSC assignments exist for all subjects$/ do
  PscV1.default_psc_connection("test user").stub!(:assignment_exists?).and_return(true)
end

...and what these stubs are supposed to do is make the Cucumber scenario think that the API calls are working. However, the stubs don't seem to persist between steps, so further steps in my scenario don't get the stubbed return values, they try to make an actual API call, and therefore they fail.

Is there a way to get stubs to persist at least as long as an entire scenario? I've used stubs successfully in other Cucumber tests, so I know they work in general, but this is the first time I've written a Cucumber step whose entire purpose is to provide a stub.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

为你拒绝所有暧昧 2025-01-03 01:36:05

据我所知,他们是否坚持下去的答案很简单,“不”。

我最终编写了一个执行以下操作的组合步骤:

When /^I follow "([^\"]*)" while pretending that PSC is up and assignments exists for all users$/ do |link_text|
  PscV1.stub!(:default_connection_is_up?).and_return(true)
  PscV1.default_psc_connection("test user").stub!(:assignment_exists?).and_return(true)
  click_link link_text
end

......有效。不幸的是,它不允许我重用存根,作为它们自己的步骤,但它有效。

更新 您也许可以通过将存根分配给类级别变量来绕过此限制,该变量可以从同一场景中的其他步骤访问。

As far as I can tell, the answer to whether or not they persist is, quite simply, "no".

I wound up writing a combined step that did the following:

When /^I follow "([^\"]*)" while pretending that PSC is up and assignments exists for all users$/ do |link_text|
  PscV1.stub!(:default_connection_is_up?).and_return(true)
  PscV1.default_psc_connection("test user").stub!(:assignment_exists?).and_return(true)
  click_link link_text
end

...which works. It doesn't allow me to reuse the stubs, as their own steps, unfortunately, but it works.

UPDATE You might be able to get around this limitation by assigning the stub to a class level variable, which is accessible from other steps within the same scenario.

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