Skip to content

expect_request should take into account times() #1108

Open

Description

@grosser
r = stub_request(...).times(3)
assert_requested r

should validate that it was called 3 times, and not 1 time like it does now

just need to store the sum of times

WebMock::RequestStub.prepend(Module.new do
  attr_reader :times_to_expect
  def times(t)
    @times_to_expect ||= 0
    @times_to_expect += t
    super
  end
end)

and then validate using that number (unless times kwarg was given)

... lmk if you agree and I can send a PR

Activity

  1. changed the title [-]expect_request should take into account `times`[/-] [+]expect_request should take into account `times()`[/+] on Oct 20, 2025
  2. bblimke commented on Oct 30, 2025

    @bblimke
    Owner

    @grosser Thanks for the suggestion!

    However, times on RequestStub serves a different purpose, it defines how many times a response should be returned before moving to the next declared response. It's not meant to specify how many times the request matching this stub is expected to be made. Please see: https://github.com/bblimke/webmock/?tab=readme-ov-file#specifying-number-of-times-given-response-should-be-returned

    When you pass a stub to assert_requested, it's just being used as a request pattern, not as an expectation count. It's just a convenience support for RequestStub parameter.

    Have you tried this instead?

    assert_requested r, times: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions