2015-08-16 70 views
0

在我的一個測試中,我想執行before :all操作,並且還需要測試在此before :all操作中未調用的方法。有沒有辦法做到這一點?在RSpec中調用stubbed方法時設置變量

例如我希望能夠到:

before :all do 
    @bar_called = false 
    # set @bar_called to true if bar is called on an instance of class Foo 
    do_other_stuff 
end 

,然後在以後我的規格之一,預計@bar_called == false

回答

0

你也可以實現與塊的模擬,然後設置@bar_called。現在

expect(obj).to receive(:my_method) { @bar_called = true }

,你可能要考慮設置期望的模擬/存根代替

expect(obj).to receive(:my_method).exactly(0).times

RSpec-Mocks documentation on receive counts