如何在 Cucumber 测试中模拟/存根模型

发布于 2024-08-03 14:36:39 字数 231 浏览 2 评论 0原文

场景如下。我的订单模型有一个 after_create ,它联系远程支付网关以检索支付 URL。在我的 Cucumber 测试中,我不想执行此操作,而是返回任意 URL。我当前的黄瓜测试如下所示:

假设有一个产品“Product X” 当我输入我的凭据时 然后我点击“立即订购” 然后我应该被重定向到“任意 url”

问题是我在哪里/如何确保我的订单模型正确设置 url 并且不会联系远程支付网关?

The scenario is as follows. My Order model has an after_create that contacts a remote payment gateway to retrieve a payment URL. In my Cucumber tests I don't want to perform this action, but return an arbitrary URL. My current cucumber tests looks like this:

Given there is a product "Product X"
When I enter my credentials
And I click "Order Now"
Then I should be redirected to "arbitrary url"

The problem is where/how do I make sure that my order model sets the url correctly and does not contact the remote payment gateway?

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

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

发布评论

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

评论(3

猫七 2024-08-10 14:36:39

wiki 还提供了一些有关存根的提示

The wiki also has some tips on stubbing.

━╋う一瞬間旳綻放 2024-08-10 14:36:39

在 features/support/env.rb 中,我对订单模型进行了猴子修补以设置任意 URL。这也可以用 Mocha 或其他东西来完成,但在这种情况下没有意义。

在我的步骤中,我可以检查正确重定向的响应,如下所示:

Then /^I should be redirected to the payment gateway$/ do
  response.status.should eql("302 Found")
  response.location.should eql(Order.last.payment_url)
end

希望这对其他人也有帮助。我仍然想知道是否有更好/更干净的方法来实现这个目标。

In features/support/env.rb I monkey-patched my Order model to set the arbitrary URL. This could possible be done with Mocha or something else as well, but there is not point in this case.

In my steps I can check the response for the correct redirect like this:

Then /^I should be redirected to the payment gateway$/ do
  response.status.should eql("302 Found")
  response.location.should eql(Order.last.payment_url)
end

Hope this helps for others as well. I'd still like to know if there's a better/cleaner way of achieving this goal.

帅气称霸 2024-08-10 14:36:39

如果我理解您想要正确执行的操作,请查看 FakeWeb

If I understand what you are trying to do correctly, have a look at FakeWeb.

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