samedi 17 août 2019

How to filter out one method call from many others with `expect().to receive()` in RSpec

I have such a code:

class ClassB
  def print_letter(arg)
  end
end

class ClassA
  def self.my_method
    ClassB.print_letter("a")
    ClassB.print_letter("b")
  end
end

RSpec.describe ClassA do
  describe "self.my_method" do
    it "prints a" do
      allow(ClassB)
      expect(ClassB).to receive(:print_letter).once.with("a")
      described_class.my_method
    end
  end
end

And I'm getting a failure:

#<ClassB (class)> received :print_letter with unexpected arguments
  expected: ("a")
       got: ("b")

Can I do anything with that? Is there any way to force the receive method to analyze all method calls and pick the one where arguments match, not only the last one? BTW, this behavior seems confusing to me.

Aucun commentaire:

Enregistrer un commentaire