rspec存根已经存在的方法

时间:2014-04-16 11:13:01

标签: ruby-on-rails rspec

我在模型上有一个方法(send_to),我想避免在一个特定的测试中调用它。但它总是被执行。

我这样做。

subject { create(:batch_with_jobs) } #Batch hasMany Jobs
...
it 'raise if all jobs are not in right state' do
  Job.stub(:sent_to){true} #first attempt, doesnt work
  Job.any_instance.stub(:sent_to).and_return(true)  #second attempt, doesnt work

  subject.jobs.first.update_attribute :state, :error_submitting
  expect{subject.commit}.to raise_error
end

commit方法继续执行原始的send_to方法。

编辑:是的,问题是因为我正在寻找SENT_TO而且它应该是SEND_TO。

1 个答案:

答案 0 :(得分:1)

对您的代码发表两条评论。

  1. 正如@fivedigit所说,您存根sent_to,但声称该方法名为send_to
  2. 你的规范描述很难阅读,也许有些事情就像#34;当没有正确的状态时会引发错误"或者"当所有作业都处于错误状态时引发错误"。