Class: WebMock::Fixtures::Manager
- Inherits:
-
Object
- Object
- WebMock::Fixtures::Manager
- Defined in:
- lib/webmock/fixtures/manager.rb
Overview
Class used to manage WebMock fixtures
Instance Attribute Summary collapse
-
#started_fixtures ⇒ Hash
readonly
Fetch the started ‘WebMock::RequestStub`s that belong to this manager.
Class Method Summary collapse
-
.fixtures ⇒ Hash
Retrieve a hash of registered fixtures available to this Manager.
-
.inherited(subclass) ⇒ Object
Ensure every subclass of ‘Manager` sets `@fixtures` to `{}` (by default it will be `nil`).
-
.register_fixture(name, verb, pattern, response) ⇒ Object
Preload a web fixture which can then be started via ‘Manager.run`.
-
.register_fixture_file(name, verb, pattern, file_name) ⇒ Object
Preload a web fixture from a file which can then be started via ‘Manager.run`.
-
.reset! ⇒ Object
Reset this manager by removing all previously registered fixtures.
-
.run(fixture_names) ⇒ Manager
Start the named preloaded web fixtures.
Instance Method Summary collapse
-
#[](fixture_name) ⇒ WebMock::RequestStub
Fetch the ‘WebMock::RequestStub` for any started fixtures.
-
#[]=(fixture_name, stub) ⇒ Object
Store an instance of ‘WebMock::RequestStub` for a given fixture.
-
#initialize ⇒ Manager
constructor
Constructor for Manager.
Constructor Details
#initialize ⇒ Manager
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_fixtures ⇒ Hash (readonly)
Fetch the started ‘WebMock::RequestStub`s that belong to this manager
11 12 13 |
# File 'lib/webmock/fixtures/manager.rb', line 11 def started_fixtures @started_fixtures end |
Class Method Details
.fixtures ⇒ Hash
Retrieve a hash of registered fixtures available to this Manager
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`)
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`
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`
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
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 = @fixtures[fixture_name] stub = WebMock::API.stub_request([:verb], [:pattern]) stub.to_return([: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
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
31 32 33 |
# File 'lib/webmock/fixtures/manager.rb', line 31 def []=(fixture_name, stub) @started_fixtures[fixture_name] = stub end |