Rails and Webmock: Dumping the result data to help writing WebMock stubs
As a default I set WebMock to disable all network interactions in my spec_helper.rb:
RSpec.configure do |config|
...
config.before(:each) do
...
WebMock.disable_net_connect!
...
end
end
I really do not want our test code to be dependant of network availability and I want them to run fast.
Sometimes, I need to write a test that connect to a real world service and I want a little help in understanding what I should mock.
I thus allow WebMock to use the network and dump the trafic I would like to analyse for mocking:
before(:each) do
WebMock.allow_net_connect! # TODO Mock
WebMock.after_request do |request_signature, response|
puts "Request #{request_signature} was made.\nResponse: Status = #{response.status}\n Header = #{JSON.pretty_generate response.headers}\n Body = #{response.body}"
end
end
With the relevant dump, you can mock various cases in a complex third-party API quite easily.
Do not forget that you will have to review the third-party API from time to time to check if it did not change.
Written by Mickaël Rémond
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#