在RSpec中调用stubbed方法时设置变量

时间:2015-08-16 17:03:19

标签: ruby-on-rails ruby rspec

在我的一项测试中,我想执行before :all操作,并且还需要测试在此before :all操作中未调用方法。有没有办法做到这一点?

e.g。我希望能够:

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

1 个答案:

答案 0 :(得分: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

相关问题