在编写 RSpec 测试时,我发现自己写了很多看起来像这样的代码,以确保在执行测试期间调用了一个方法(为了争论,我只是说我真的不能查询调用后对象的状态,因为方法执行的操作不容易看到效果)。
describe "#foo"
it "should call 'bar' with appropriate arguments" do
called_bar = false
subject.stub(:bar).with("an argument I want") { called_bar = true }
subject.foo
expect(called_bar).to be_true
end
end
我想知道的是:还有比这更好的语法吗?我是否缺少一些时髦的 RSpec 令人敬畏的东西,可以将上面的代码减少到几行? should_receive
听起来它应该这样做,但进一步阅读它听起来并不完全是它所做的。
最佳答案
it "should call 'bar' with appropriate arguments" do
expect(subject).to receive(:bar).with("an argument I want")
subject.foo
end
关于ruby-on-rails - RSpec:如何测试一个方法是否被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21262309/