如何使用rspec调用方法

时间:2014-04-09 11:24:20

标签: ruby rspec

我想检查是否使用rspec调用方法。

我遵循了这条指令。 https://relishapp.com/rspec/rspec-mocks/v/3-0/docs/message-expectations/receive-counts

我有这样的课.Foo。

class Foo
  def run
    bar
  end
  def bar
  end
end

这是它的spec文件。

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

但是这个错误就失败了。

  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)>'

如何为此run方法编写rspec测试?

1 个答案:

答案 0 :(得分:2)

您需要实际调用正在测试的方法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