在编写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
Run Code Online (Sandbox Code Playgroud)
我想知道的是:有比这更好的语法吗?我是否缺少一些时髦的RSpec非常棒,可以将上面的代码减少到几行?should_receive听起来它应该这样做但是进一步阅读它听起来并不完全是它的作用.