【发布时间】:2018-01-24 17:25:55
【问题描述】:
我正在尝试在黄瓜测试中模拟外部 API 故障。调用外部 API 的控制器操作通过名为 post_to_connect 的控制器方法来实现。该服务在测试期间在本地运行,因此快乐路径测试正常工作。但是,我的控制器操作如下所示:
def create_connection_test
post_to_connect # posts to external API
redirect_to '/success'
rescue => e
@error = e.to_s
redirect_to '/connection-test/new'
end
我需要强制post_to_connect 引发错误,以便我可以为悲伤的路径编写黄瓜测试。这是我尝试过的:
功能文件内部:
Scenario: API failure is handled gracefully
Given I am on the LDAP Connect stage
And The connect API will fail
And I click "Connect"
Then I see the correct error message
步骤定义内部:
Then(/^The connect API will fail$/) do
allow_any_instance_of(LdapController).to receive(:post_to_connect)
.and_raise("some error")
end
这不会导致控制器方法post_to_connect抛出错误?我做错了什么,如何正确完成覆盖post_to_connect?
【问题讨论】:
-
因为请求是从与黄瓜测试不同的进程执行的,所以它们不共享相同的内存。您不能像在单元测试中那样对它进行存根。您必须找到另一种方法来使您的 API 调用失败。
-
在 Java 中,我们可以使用 MockServer 或 WireMock 来模拟(实际上是存根)外部服务。 Ruby 有没有类似的工具?
标签: ruby-on-rails rspec cucumber