WebMock fixtures
Library for managing WebMock fixtures.
Documentation.
Installation
gem install webmock-fixtures
or with Bundler
gem "webmock-fixtures"
Getting Started
WebMock fixtures provides a way to pre-define all of your fixtures ahead of time and start them easily during tests.
require "rspec"
require "webmock/fixtures"
require "webmock/rspec"
WebMock::Fixtures::Manager.register_fixture(
:get_example, :get, %r{www.example.org}, :body => "Hello World")
RSpec.describe do
describe "a web request to www.example.org" do
let!(:manager) { WebMock::Fixtures::Manager.run([:get_example]) }
it "should respond with the fixture body" do
response = Net::HTTP.get("www.example.org", "/")
expect(response).to(eq("Hello World"))
stub = manager[:get_example]
expect(stub).to(have_been_requested.once)
end
end
end
As well, by subclassing WebMock::Fixtures::Manager
you can easily separate and organize your test fixtures. For example:
class GoogleManager < WebMock::Fixtures::Manager
@fixtures = {
:get_homepage => {
:verb => :get,
:pattern => %r{www.google.com},
:response => {
:body => "Google",
},
},
}
end
class ElasticsearchManager < WebMock::Fixtures::Manager
end
ElasticsearchManager.register_fixture_file(
:search_people, :get, %r{localhost:9002/people/person/_search}, "/path/to/file.raw")
google_manager = GoogleManager.run([:get_homepage])
elasticsearch_manager = ElasticsearchManager.run([:search_people])
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.
License
Copyright (c) 2015 Underdog.io
Licensed under the MIT license.