如何在 Cucumber 测试中模拟/存根模型
场景如下。我的订单模型有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
wiki 还提供了一些有关存根的提示。
The wiki also has some tips on stubbing.
在 features/support/env.rb 中,我对订单模型进行了猴子修补以设置任意 URL。这也可以用 Mocha 或其他东西来完成,但在这种情况下没有意义。
在我的步骤中,我可以检查正确重定向的响应,如下所示:
希望这对其他人也有帮助。我仍然想知道是否有更好/更干净的方法来实现这个目标。
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:
Hope this helps for others as well. I'd still like to know if there's a better/cleaner way of achieving this goal.
如果我理解您想要正确执行的操作,请查看 FakeWeb。
If I understand what you are trying to do correctly, have a look at FakeWeb.