0
votes

I want to check if a method is called with rspec.

I followed this instruction. https://relishapp.com/rspec/rspec-mocks/v/3-0/docs/message-expectations/receive-counts

I have a class Foo like this.

class Foo
  def run
    bar
  end
  def bar
  end
end

And this is spec file for it.

require_relative 'foo'

describe Foo do
  let(:foo){ Foo.new }
  describe "#run" do
    it "should call bar" do
      expect(foo).to receive(:bar)
    end
  end
end

But it fails with this error.

  1) Foo#run should call foo
     Failure/Error: expect(foo).to receive(:bar)
       (#<Foo:0x007f8f9a22bc40>).bar(any args)
           expected: 1 time with any arguments
           received: 0 times with any arguments
     # ./foo_spec.rb:7:in `block (3 levels) in <top (required)>'

How can I write rspec test for this run method?

1

1 Answers

2
votes

You need to actually call the method under test, run.

describe Foo do
  let(:foo){ Foo.new }
  describe "#run" do
    it "should call bar" do
      expect(foo).to receive(:bar)
      foo.run # Add this
    end
  end
end