2013-04-23 90 views
25

搜索了Relish文檔,但沒有在RSpec中找到未壓縮的方式。有沒有辦法在RSpec中unub?

這可能嗎?

+0

你能解釋一下你爲什麼要取消某些東西嗎?也許有更好的方法。 – 2013-04-26 22:43:18

回答

20

rspec-mock代碼表明您可以調用unstub方法。我引述:

# Removes a stub. On a double, the object will no longer respond to 
    # `message`. On a real object, the original method (if it exists) is 
    # restored. 
    # 
    # This is rarely used, but can be useful when a stub is set up during a 
    # shared `before` hook for the common case, but you want to replace it 
    # for a special case. 
    def unstub(message) 
    ::RSpec::Mocks.space.proxy_for(self).remove_stub(message) 
    end 
77

隨着新expect語法,unstub已被棄用。你可以這樣做:

# stub 
allow(SomeClass).to receive(:a_method) 

# do something... 

# unstub 
allow(SomeClass).to receive(:a_method).and_call_original 

如果第一allow包含.with或塊,我相信它還是會進行到下一個呼叫,所以接下來allow並不清楚這些事情。

+2

謝謝你。你有沒有參考這個文檔或代碼中的位置? – steel 2014-09-12 17:56:49

+2

這應該被標記爲導軌4的正確答案rspec 3.0 – 2014-11-11 20:49:14

+0

版本Rspec 3的文檔。https://relishapp.com/rspec/rspec-mocks/docs/configuring-responses/calling-the-original-implementation – counterbeing 2015-08-26 22:34:32