2015-10-13 32 views
0

我有一個存根對特定的類方法是有效的我的大多數測試。我在模塊之前設置了模擬模式,以便對測試進行干預。我想刪除一個測試無效的存根。我如何使用RSpec來做到這一點?刪除expect_any_instance_of RSpec

代碼

before :all do 
expect_any_instance_of(Foo).to receive(:callback) 
end 

it 'does a callback on this test' do 
Foo.new 
end 

it 'does it in this too!' do 
Foo.new.other 
end 

it 'doesnt do it in this one' do 
# how do i remove the stub??? 
end 

回答

1

你可以使用不同的context的,所以你可以形容1點的行爲與另一個像這樣:

describe 'something' do 
    context 'valid with callback' do 
    before :each do 
     expect_any_instance_of(Foo).to receive(:callback) 
    end 

    it 'does a callback on this test' do 
     Foo.new 
    end 

    it 'does it in this too!' do 
     Foo.new.other 
    end 
    end 

    context 'invalid with callback' do 
    it 'doesnt do it in this one' do 
     # how do i remove the stub??? 
    end 
    end 
end 

在上面的代碼中before :each塊僅是局部的valid with callback上下文,所以你可以在那裏測試那個孤立的行爲。

0

更新:expect_any_instance_of(Foo).to receive(:bar).and_call_original

但是,考慮到它可能是一種氣味,如果你是一個嘲諷類的每個實例。構建測試可能有更清晰的替代方案。