I'm writing a gem that acts as a client for a remote API, so I'm using webmock to mock out the remote API, testing with Cucumber with rspec-mock present too.
As part of my Cucumber tests I intend to stub my API in a Given
clause but then I would like to specify that the remote API is called in a Then
clause.
A really basic example would be:
Feature file
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
Step definition
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
The question: How can I verify that my google stub has been called?
Side note: I'm aware I could use the expect(a_request(...))
or expect(WebMock).to ...
syntax but my feeling is I'll be repeating what's defined in my Given
clause.
I answer this myself, though it would be good for someone to verify this is correct and/or has no major pitfalls:
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
The bits to note are the assignment of @request
and the expectation placed upon it in the Then
clause.
In a limited test of two separate scenarios this approach seems to work.