Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webmock doesn't count how many times each stub was requested #445

Open
x-yuri opened this issue Jan 9, 2015 · 3 comments
Open

webmock doesn't count how many times each stub was requested #445

x-yuri opened this issue Jan 9, 2015 · 3 comments

Comments

@x-yuri
Copy link

x-yuri commented Jan 9, 2015

Consider the following script:

require 'rspec'
require 'webmock/rspec'

describe '...' do                                                                               
  it '...' do
    stub1 = stub_request(:post, 'http://example.com').to_return(body: 's1')
    stub2 = stub_request(:post, 'http://example.com').with(body: hash_including(a: '1'))
    pp Net::HTTP.post_form(URI('http://example.com'), a: 1).body
    pp WebMock::RequestRegistry.instance.requested_signatures.hash
    pp stub1.request_pattern
    pp stub2.request_pattern
    pp WebMock::RequestRegistry.instance.times_executed(stub1.request_pattern)
    pp WebMock::RequestRegistry.instance.times_executed(stub2.request_pattern)
    expect(stub1).to have_been_requested
    expect(stub2).to have_been_requested
  end
end

Output:

"s2"
{#<WebMock::RequestSignature:0x007f60a57cc538
  @body="a=1",
  @headers=
   {"Accept-Encoding"=>"gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
    "Accept"=>"*/*",
    "User-Agent"=>"Ruby",
    "Host"=>"example.com",
    "Content-Type"=>"application/x-www-form-urlencoded"},
  @method=:post,
  @uri=#<Addressable::URI:0x3fb052bd2508 URI:http://example.com:80/>>=>1}
#<WebMock::RequestPattern:0x007f60a5827c08
 @body_pattern=nil,
 @headers_pattern=nil,
 @method_pattern=#<WebMock::MethodPattern:0x007f60a58278e8 @pattern=:post>,
 @uri_pattern=
  #<WebMock::URIStringPattern:0x007f60a5827870
   @pattern=#<Addressable::URI:0x3fb052c08edc URI:http://example.com:80/>,
   @query_params=nil>,
 @with_block=nil>
#<WebMock::RequestPattern:0x007f60a580e118
 @body_pattern=
  #<WebMock::BodyPattern:0x007f60a57fe880 @pattern=hash_including({"a"=>"1"})>,
 @headers_pattern=nil,
 @method_pattern=#<WebMock::MethodPattern:0x007f60a580de98 @pattern=:post>,
 @uri_pattern=
  #<WebMock::URIStringPattern:0x007f60a580dc68
   @pattern=#<Addressable::URI:0x3fb052c063bc URI:http://example.com:80/>,
   @query_params=nil>,
 @with_block=nil>
1
1
.

Finished in 0.00682 seconds (files took 0.36602 seconds to load)
1 example, 0 failures

I expected only stub2 to have been requested. The same goes not only for body, but also for headers and with block matchers. Is it just me or is this a bug? At least it makes expectations like expect(stub1).to have_been_requested sound misleading.

@bblimke
Copy link
Owner

bblimke commented Jan 9, 2015

Only stub2 was used.

stub1 pattern is a superset of stub2 pattern, so if you ask WebMock::RequestRegistry.instance.times_executed(stub1.request_pattern), it will match.

expect(stub1).to have_been_requested is just a convenience method to not have to write the same pattern twice in stub and in expectation. It doesn't check which stub exactly was used but matches pattern of that stub against executed request signatures.

@bblimke
Copy link
Owner

bblimke commented Jan 9, 2015

If you want stub1 to have empty body and be different than stub2 rather than omitting body (and saying, 'I don't care about body'), declare it with empty body body => {}.

@x-yuri
Copy link
Author

x-yuri commented Jan 9, 2015

If you want stub1 to have empty body and be different than stub2 rather than omitting body (and saying, 'I don't care about body'), declare it with empty body body => {}.

Yeah, I thought as much. It's just that this exact phrasing is misleading: expect(stub1).to have_been_requested. UPD More correct way would be: expect_request_matching(stub1.request_pattern).to have_been_made, or something along those lines. Additionally, doesn't it make sense to add this ability? To make expectations specifically on stubs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants