2014-05-09 13 views
1

使用Rspec,有沒有一種方法可以將所有對除第一個方法之外的方法進行調用?我可以將所有呼叫存根到第一個方法嗎?

例子:

class Book 
    def foo 
    "Book#foo()" 
    end 
end 

在規格:

it 'x' do 
    # .... (do something to stub all but the first method call to `Book#foo`).... 

    b = Book.new 
    expect(b.foo).to eq('Book#foo()') 
    expect(b.foo).to be_nil 
    expect(b.foo).to be_nil 
end 

有人嗎?

+0

你能舉個例子,這樣我們就可以看到你的意思了嗎? –

+0

非常感謝。我正在學習這些現在 - 一天..用理論來理解東西非常需要例子。因此,我問.. –

回答

0

是的,它是可行的。我沒有真正測試過這個,但是如果我的記憶爲我服務,那麼應該這樣做:

it 'does some funky stubbing' do 
    book = Book.new 

    # the first time it will not be stubbed, all subsequent times it will return nil 
    allow(book).to receive(:foo) { book.foo, nil } 

    expect(book.foo).to eq('Book#foo()') 
    expect(book.foo).to be_nil 
    expect(book.foo).to be_nil 
end 
相關問題