Skip to content

Commit

Permalink
Clarify faraday's on_data callback tests (#933)
Browse files Browse the repository at this point in the history
* Highlight callback code

Easier to spot

* Make test action explicit

Clarify that we have to pass on_data callback to request

* Clarify names

* Remove redundant arguments

* Document on_data callback expectations

* Clarify names

* Remove redundant assertion

* Document response body expectations

* Remove redundant test
  • Loading branch information
nellirx committed Apr 19, 2022
1 parent d874932 commit 548ebff
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions spec/lib/vcr/library_hooks/faraday_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,26 @@
end

context 'when using on_data callback' do
def make_request
def make_request(&on_data_callback)
VCR.use_cassette('no_body') do
conn = Faraday.new(:url => "http://localhost:#{VCR::SinatraApp.port}") do |builder|
builder.request :url_encoded
builder.adapter :net_http
end
conn.get("localhost_test") do |request|
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
on_data_capture.push [chunk, overall_received_bytes]
end
request.options.on_data = on_data_callback
end
end
end

let(:on_data_capture) { [] }
it { expect { |b| make_request(&b) }.to yield_with_args('Localhost response', 18) }
it { expect(make_request {|_,_|}.body).to eq 'Localhost response' }

it 'records and replays correctly' do
expect(on_data_capture).to receive(:push).exactly(2).times.with(["Localhost response", 18])
context 'after recording' do
before { make_request {|_, _|} }

recorded = make_request
played_back = make_request

expect(recorded.body).to eq('Localhost response')
expect(played_back.body).to eq(recorded.body)
it { expect { |b| make_request(&b) }.to yield_with_args('Localhost response', 18) }
it { expect(make_request {|_,_|}.body).to eq 'Localhost response' }
end
end
end
Expand Down

0 comments on commit 548ebff

Please sign in to comment.