Matching on keyword arguments in a with() constraint

355 views
Skip to first unread message

Olle Jonsson

unread,
Aug 24, 2022, 3:21:34 AM8/24/22
to rs...@googlegroups.com
Hello,

Given receiver has a method foo(first_name:, last_name:)

We could check for calls to that with:

Use a hash_including Matcher:
expect(receiver).to receive(:foo).with(hash_including(first_name: "Alan"))

Mention every keyword argument in the expectation:
expect(receiver).to receive(:foo).with(first_name: "Alan", last_name: anything)

Is there a way to match "just a few keywords"?

Weak ideas:
- Perhaps locally aliasing hash_including to keywords_including

Anyone got anything else? Is this a real wart, or is it something that'll be invisible to the reader after they've fully entered Ruby 3?

Thanks for considering this very minor issue!

    /Olle

Jon Rowe

unread,
Aug 24, 2022, 3:31:14 AM8/24/22
to rspec
I'd love there to be a `keywords_including` matcher here, `hash_including` works on some Rubies but it doesn't check that they are actually keywords, leaving that up to `with` itself, we can actually check if its a keywords hash we're inspecting though.

Cheers
Jon
Reply all
Reply to author
Forward
0 new messages