Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

webmock-fixtures

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webmock-fixtures

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

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"

# 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.

FAQs

Package last updated on 17 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc