Skip to content

Allow receive_message_chain to be constained by arguments

Created by: JonRowe

With the old should syntax you could constrain stub and stub_chain calls by their arguments using with, e.g.

describe do
  let(:object) { double }

  specify do
    object.stub(:call).with(1) { 2 }
    object.stub(:call).with(3) { 4 }
    expect(object.call 1).to eq 2
    expect(object.call 3).to eq 4
  end

  specify do
    object.stub_chain(:call, :call).with(1) { 2 }
    object.stub_chain(:call, :call).with(3) { 4 }
    expect(object.call.call 1).to eq 2
    expect(object.call.call 3).to eq 4
  end
end

But the equivalent in the expect syntax:

  specify do
    allow(object).to receive(:call).with(1) { 2 }
    allow(object).to receive(:call).with(3) { 4 }
    expect(object.call 1).to eq 2
    expect(object.call 3).to eq 4
  end

  specify do
    allow(object).to receive_message_chain(:call, :call).with(1) { 2 }
    allow(object).to receive_message_chain(:call, :call).with(3) { 4 }
    expect(object.call.call 1).to eq 2
    expect(object.call.call 3).to eq 4
  end
end

Fails on the later receive_messaage_chain because we don't define with.

This makes that change, and supports constraining arguments on message chains. Given the simplicity of the "fix" I'm inclined to support this, WDYT @myronmarston

Fixes #696 (closed).

Merge request reports