Question

I want to ensure that my Foo.bar method is called with true at some point during my test. So far I've only been able to assert against the first call to Foo.bar. I need to assert against any call.

This is what I have so far but doesn't work:

  expect(Foo).to receive(:bar).at_least(:once).with("true")

  Foo.bar("false")
  Foo.bar("false")
  Foo.bar("true")
  Foo.bar("false")

It fails on the first Foo.bar because "false" doesn't match my "true" expectation. How would you rewrite this to pass because Foo.bar("true") is called at some point during the test?

Was it helpful?

Solution

I think that in this case you would need to do what I think of as the method stubbing equivalent of as_null_object:

describe Foo
  describe 'testing .bar multiple times' do
    before do
      allow(Foo).to receive(:bar) # stub out message
    end

    it "can determine how many times it has been called with 'true'" do
      expect(Foo).to receive(:bar).at_least(:once).with("true")
      expect(Foo).to receive(:bar).at_most(:once).with("true")
      Foo.bar("false")
      Foo.bar("false")
      Foo.bar("true")
      Foo.bar("false")
    end
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top