Skip to content

allow has_received to take a block to match against passed arguments.

Created by: timcowlishaw

As discussed in @rspec/rspec-mocks#364, it'd be great to be able to pass a block to has_received in order to make more specific assertions about the arguments with which the mocked method was called, for example:

    let(:missile_silo) { double(:missile_silo, :fire_missiles! => true) }

    it "calls the missile silo with a user authorized to fire missiles" do
      missile_command = MissileCommand.new(missile_silo)
      missile_command.start_war!
      expect(missile_silo).to have_received(:fire_missiles!) do |authorizing_user|
        expect(authorizing_user.rank).to eq(:commander)
      end
    end

This patch implements this behaviour, and adds specs for argument matching in this style on both the Receive and HasReceived matchers.

Merge request reports