2013-04-23 91 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