Class: WebMock::Fixtures::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/webmock/fixtures/manager.rb

Overview

Class used to manage WebMock fixtures

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Constructor for Manager



14
15
16
17
# File 'lib/webmock/fixtures/manager.rb', line 14

def initialize
  # We will use `@started_fixtures` to store our started fixtures
  @started_fixtures = {}
end

Instance Attribute Details

#started_fixturesHash (readonly)

Fetch the started ‘WebMock::RequestStub`s that belong to this manager

Returns:

  • (Hash)

    a Hash of fixture names mapped to their started ‘WebMock::RequestStub`



11
12
13
# File 'lib/webmock/fixtures/manager.rb', line 11

def started_fixtures
  @started_fixtures
end

Class Method Details

.fixturesHash

Retrieve a hash of registered fixtures available to this Manager

Returns:

  • (Hash)

    a hash mapping the registered name to a hash of properties e.g. ‘{ :get_example => { :pattern => %rexampleexample.org, :verb => :get, :response => “Hello World” } }`



61
62
63
# File 'lib/webmock/fixtures/manager.rb', line 61

def self.fixtures
  return @fixtures
end

.inherited(subclass) ⇒ Object

Ensure every subclass of ‘Manager` sets `@fixtures` to `{}` (by default it will be `nil`)

Parameters:

  • subclass (Class)

    the class instance which is subclassing ‘Manager`



38
39
40
# File 'lib/webmock/fixtures/manager.rb', line 38

def self.inherited(subclass)
  subclass.instance_variable_set(:@fixtures, {})
end

.register_fixture(name, verb, pattern, response) ⇒ Object

Preload a web fixture which can then be started via ‘Manager.run`

Parameters:

  • name (Symbol)

    the name to register the fixture as

  • verb (Symbol)

    symbol method to mock (e.g. ‘:get`, `:post`)

  • pattern (Regexp|String)

    URI pattern to match for this mock

  • response (String|File|Lambda|Hash)

    the response, this is the same as what would be supplied to ‘WebMock::RequestStub#to_return` please see github.com/bblimke/webmock for examples



49
50
51
52
53
54
55
# File 'lib/webmock/fixtures/manager.rb', line 49

def self.register_fixture(name, verb, pattern, response)
  @fixtures[name] = {
    :pattern => pattern,
    :verb => verb,
    :response => response,
  }
end

.register_fixture_file(name, verb, pattern, file_name) ⇒ Object

Preload a web fixture from a file which can then be started via ‘Manager.run`

Parameters:

  • name (Symbol)

    the name to register the fixture as

  • verb (Symbol)

    symbol method to mock (e.g. ‘:get`, `:post`)

  • pattern (Regexp|String)

    URI pattern to match for this mock

  • file_name (String)

    the name of the file to load the fixture response from



77
78
79
80
81
# File 'lib/webmock/fixtures/manager.rb', line 77

def self.register_fixture_file(name, verb, pattern, file_name)
  # Read the contents from the file and use as the response
  # https://github.com/bblimke/webmock/tree/v1.22.1#replaying-raw-responses-recorded-with-curl--is
  register_fixture(name, verb, pattern, File.new(file_name).read)
end

.reset!Object

Reset this manager by removing all previously registered fixtures



67
68
69
# File 'lib/webmock/fixtures/manager.rb', line 67

def self.reset!
  @fixtures = {}
end

.run(fixture_names) ⇒ Manager

Start the named preloaded web fixtures

Parameters:

  • fixture_names (Array)

    the symbol names of fixtures to load and start

Returns:

  • (Manager)

    an instance of Manager which has properties set for each started fixture

Raises:

  • (KeyError)

    if the fixture has not been preloaded via ‘::register_fixture` or `::register_fixture_file`



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/webmock/fixtures/manager.rb', line 88

def self.run(fixture_names)
  # Create a new instance to store started mocks on
  manager = new()
  fixture_names.each do |fixture_name|
    # DEV: `fetch` will fail if the key does not exist
    unless @fixtures.key?(fixture_name)
      fail(KeyError, "The fixture \"#{fixture_name}\" was not found. Please make sure to " +
                     "register it with ::register_fixture or ::register_fixture_file")
    end

    # Create the WebMock stub
    fixture_options = @fixtures[fixture_name]
    stub = WebMock::API.stub_request(fixture_options[:verb], fixture_options[:pattern])
    stub.to_return(fixture_options[:response])

    # Store the started stub on the manager instance
    manager[fixture_name] = stub
  end
  return manager
end

Instance Method Details

#[](fixture_name) ⇒ WebMock::RequestStub

Fetch the ‘WebMock::RequestStub` for any started fixtures

Parameters:

  • fixture_name (Symbol)

    the symbol name of the fixture

Returns:

  • (WebMock::RequestStub)

    the started fixture stub

Raises:

  • (KeyError)

    if the fixture requested has not been started



23
24
25
26
# File 'lib/webmock/fixtures/manager.rb', line 23

def [](fixture_name)
  # DEV: `fetch` wil raise `KeyError` if `fixture_name` does not exist
  return @started_fixtures.fetch(fixture_name)
end

#[]=(fixture_name, stub) ⇒ Object

Store an instance of ‘WebMock::RequestStub` for a given fixture

Parameters:

  • fixture_name (Symbol)

    the symbol name of the fixture started

  • stub (WebMock::RequestStub)

    the stub that was started for the fixture



31
32
33
# File 'lib/webmock/fixtures/manager.rb', line 31

def []=(fixture_name, stub)
  @started_fixtures[fixture_name] = stub
end