【发布时间】:2014-02-11 07:03:04
【问题描述】:
在编写 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 听起来它应该这样做,但进一步阅读它听起来并不完全是它的作用。
【问题讨论】:
-
@Peter Alfvin OP 在
should_receive上询问语法,所以我认为这个问题会有所帮助。
标签: ruby-on-rails ruby rspec