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

When using matcher with block, the block is executed less than expected #548

Open
rockuw opened this issue Nov 17, 2015 · 1 comment
Open

Comments

@rockuw
Copy link

rockuw commented Nov 17, 2015

Hi, with the following code:

it "test matcher with block", :focus => true do
  stub_request(:get, "xxx.com")

  10.times do
    RestClient.get("xxx.com", :headers => {'Range' => '1-100'})
  end

  expect(WebMock).to have_requested(:get, "xxx.com")
                      .with{ |req|
    puts "req"
    true
  }.times(10)
end

I expect the block to be executed 10 times. But only one "req" was printed.

However, if I send different requests each time, the behavior is as expected.

it "test matcher with block", :focus => true do
  stub_request(:get, "xxx.com")

  10.times do |i|
    RestClient.get("xxx.com", :headers => {'Range' => "#{i}-100"})
  end

  expect(WebMock).to have_requested(:get, "xxx.com")
                      .with{ |req|
    puts "req"
    true
  }.times(10)
end

What's the problem here?

@rockuw
Copy link
Author

rockuw commented Nov 17, 2015

After digging into the code, I find out the cause:
RequestRegistry uses a Util::HashCounter to store all the request signatures. When there are two request signatures that equal in the HashCounter, it will only be matched with the request pattern once.

def times_executed(request_pattern)
  self.requested_signatures.hash.select { |request_signature, times_executed|
    request_pattern.matches?(request_signature)
  }.inject(0) {|sum, (_, times_executed)| sum + times_executed }
end

Please fix it.

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

No branches or pull requests

1 participant