Skip to content

Add new argument matcher: any_of_the_following

gitlab-qa-bot requested to merge github/fork/lengarvey/any_of_the_following into master

Created by: lengarvey

Usage:

expect(object).to receive(:message).with(any_of_the_following([1,2,3]))

My use case for this is if I have code which loops over an array sending those elements to a third party. eg:

def a_method
  @array.each do |item|
    object.message(item)
  end
end

Currently to support this I could do:

array.each do |item|
  expect(object).to receive(:message).with(item)
end

Instead I would like to be able to:

expect(object).to receive(:message).
  with(any_of_the_following array).exactly(array.length).times

It also lets me more easily setup an expectation for random selection:

object.message(@array.sample)

Can have an expectation created like:

expect(object).to receive(:message).with(any_of_the_following array)

Instead of stubbing array.sample to have a deterministic response.

Merge request reports