repeated method call expectation

9 views
Skip to first unread message

Xavier Noria

unread,
Mar 20, 2019, 1:02:12 PM3/20/19
to rspec
I have a call path where a given method

    SomeClass.rate_limit(key, max_every: seconds)

is invoked several times, and I'd like to ensure that a certain choice of arguments is among those calls

    expect(SomeClass).to receive(:rate_limit).with("foo", max_every: 10.seconds)

I don't care about the rest.

I don't see how to make that pass, due to the multiple invocations of the method. The expectation fails because some other combination was found (albeit eventually the expected one is going to be present). Sometimes you can throw

    allow(SomeClass).to receive(:rate_limit)

just before the expectation, but this time, it is not enough, perhaps because there are several partial matches (but have not investigated it).

Is there a way to accomplish that?


Jon Rowe

unread,
Mar 20, 2019, 3:09:28 PM3/20/19
to rs...@googlegroups.com
Hi Xavier

allow(SomeClass).to receive(:rate_limit)

Should, er, allow, you to make any call to SomeClass.rate_limit, although adding:

expect(SomeClass).to receive(:rate_limit).with("foo", max_every: 10.seconds)

Would then restrict that particular combination to “once”, you can add `at_least(:once)` to that expectation to ease that restriction, but it shouldn’t be needed if you’re applying other combinations.

Note RSpec will complain with the above two statements if you don’t supply the later call.

Cheers
Jon Rowe
---------------------------

Xavier Noria

unread,
Mar 20, 2019, 6:24:28 PM3/20/19
to rs...@googlegroups.com
I had a bug :).
Reply all
Reply to author
Forward
0 new messages