在编写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
听起来它应该这样做,但进一步阅读,它听起来不完全是这样做的.