Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions docs/adapters/test-adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,16 @@ verify the order or count of any stub.
stubs.verify_stubbed_calls
```

After the test case is completed (possibly in an `after` hook), you should clear
the default connection to prevent it from being cached between different tests.
This allows for each test to have its own set of stubs
After the test case is completed (possibly in an `after` hook), you should reset
the stubs so each test starts with its own set. When you hold a reference to the
`Stubs` instance, call `clear` to remove every stub, including consumed ones.

```ruby
stubs.clear
```

Alternatively, if you rely on the default connection, you can clear it to prevent
it from being cached between different tests.

```ruby
Faraday.default_connection = nil
Expand Down
8 changes: 8 additions & 0 deletions lib/faraday/adapter/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ def options(path, headers = {}, &block)
new_stub(:options, path, headers, &block)
end

# Removes all stubs, including the ones that have already been consumed.
def clear
@stubs_mutex.synchronize do
@stack.clear
@consumed.clear
end
end

# Raises an error if any of the stubbed calls have not been made.
def verify_stubbed_calls
failed_stubs = []
Expand Down
18 changes: 18 additions & 0 deletions spec/faraday/adapter/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,22 @@ def make_request
end
end
end

describe '#clear' do
it 'removes pending stubs' do
stubs.clear
expect { connection.get('/hello') }.to raise_error(described_class::Stubs::NotFound)
end

it 'removes already consumed stubs' do
expect(connection.get('/hello').body).to eq('hello')
stubs.clear
expect { connection.get('/hello') }.to raise_error(described_class::Stubs::NotFound)
end

it 'leaves the stubs empty' do
stubs.clear
expect(stubs).to be_empty
end
end
end