No commit activity in last 3 years
No release in over 3 years
Library to help manage WebMock fixtures
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

< 11.0.0, >= 10.0.0
< 3.4.0, >= 3.3.0
< 0.35.0, >= 0.34.0

Runtime

< 2.0.0, >= 1.0.0
 Project Readme

WebMock fixtures

Gem Version Build Status

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"

# Register our fixture for www.example.org
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
    # Start the registered fixtures
    # DEV: `WebMock` will auto-reset after each test:
    #   https://github.com/bblimke/webmock/blob/v1.22.3/lib/webmock/rspec.rb#L23-L31
    let!(:manager) { WebMock::Fixtures::Manager.run([:get_example]) }

    it "should respond with the fixture body" do
      # Assert that an HTTP request gets the expected response
      response = Net::HTTP.get("www.example.org", "/")
      expect(response).to(eq("Hello World"))

      # Assert that our stub was called
      # DEV: `stub` is the created `WebMock::RequestStub` for the started fixture
      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:

# Manager to store fixtures associated with Google
class GoogleManager < WebMock::Fixtures::Manager
  # Define fixtures in class definition
  @fixtures = {
    :get_homepage => {
      :verb => :get,
      :pattern => %r{www.google.com},
      :response => {
        :body => "Google",
      },
    },
  }
end

# Manager to store fixtures associated with Elasticsearch
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.