0

我正在编写一个充当远程 API 客户端的 gem,因此我使用 webmock 来模拟远程 API,并使用带有 rspec-mock 的 Cucumber 进行测试。

作为我的 Cucumber 测试的一部分,我打算在一个子句中存根我的 API,Given但是我想指定在一个Then子句中调用远程 API。

一个非常基本的例子是:

特征文件

Scenario: Doing something that triggers a call
  Given I have mocked Google
  When I call my library
  Then it calls my Google stub
  And I get a response back from my library

步骤定义

Given /I have mocked my API/ do
  stub_request(:get, 'www.google.com')
end

When /I call my library/ do
  MyLibrary.call_google_for_some_reason
end

Then /it calls my Google stub/ do
  # Somehow test it here
end

问题: 如何验证我的谷歌存根已被调用?

旁注:我知道我可以使用expect(a_request(...))orexpect(WebMock).to ...语法,但我的感觉是我将重复我的Given子句中定义的内容。

4

1 回答 1

1

我自己回答这个问题,尽管有人验证这是正确的和/或没有重大缺陷会很好:

Given /I have mocked my API/ do
  @request = stub_request(:get, 'www.google.com')
end

Then /it calls my Google stub/ do
  expect(@request).to have_been_made.once
end

需要注意的是在子句@request中对它的赋值和期望。Then

在对两个独立场景的有限测试中,这种方法似乎有效。

于 2015-07-23T13:19:17.110 回答