require 'rspec' RSpec.describe Object do subject { Object.new } before do # Stub a method so we can spy on it allow(subject).to receive(:a_method) # Call it twice with alpha and once with beta subject.a_method("alpha") subject.a_method("alpha") subject.a_method("beta") end context "with implicit `once`" do it "fails with a misleading message" do expect(subject).to have_received(:a_method).with("alpha") # 1) Object with implicit `once` fails with a misleading message # Failure/Error: expect(subject).to have_received(:a_method).with("alpha") # # received :a_method with unexpected arguments # expected: ("alpha") # got: ("beta") # # ./argument_expectations.rb:18:in `block (3 levels) in ' end end context "with explicit `once`" do it "fails with a helpful message" do expect(subject).to have_received(:a_method).with("alpha").once # 2) Object with explicit `once` fails with a helpful message # Failure/Error: expect(subject).to have_received(:a_method).with("alpha").once # (#).a_method(no args) # expected: 1 time with arguments: ("alpha") # received: 2 times # # ./argument_expectations.rb:24:in `block (3 levels) in ' end end context "with explicit `at_least(:once)`" do it "passes" do expect(subject).to have_received(:a_method).with("alpha").at_least(:once) end end context "with explicit `at_least(3).times`" do it "fails with a misleading message" do expect(subject).to have_received(:a_method).with("alpha").at_least(3).times # 3) Object with explicit `at_least(3).times` fails with a misleading message # Failure/Error: expect(subject).to have_received(:a_method).with("alpha").at_least(3).times # # received :a_method with unexpected arguments # expected: ("alpha") # got: ("beta") # # ./argument_expectations.rb:50:in `block (3 levels) in ' end end end