Skip to content

Added `duck_type_including` ArgumentMatcher

Created by: xtrasimplicity

Background

If you are working with a hash, you can use:

allow(obj).to receive(:message).with(hash_including(name: 'Fred', last_name: 'Flintstone'))

If you are working with objects, you can use:

allow(obj).to receive(:message).with(duck_type(:name, :last_name))

But what if you want to combine these, and allow any object that both responds to name and last_name and returns Fred and Flintstone, respectively? That's where the duck_type_including argument matcher comes in. :)

Usage

allow(myObj).to receive(:message).with(duck_type_including(name: 'Fred', last_name: 'Flintstone'))

With the above, all of the following will pass:

myObj.message(OpenStruct.new(name: 'Fred', last_name: 'Flintstone'))
myObj.message(OpenStruct.new(name: 'Fred', last_name: 'Flintstone', age: 20))

This change is Reviewable

Merge request reports