2014-04-09 69 views
0

我想檢查一個方法是否用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 '富'

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測試?

回答

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 
相關問題