Arnold Roa
Arnold Roa

Reputation: 7718

rspec stub a method in an already imp

I have a method (send_to) on model that I want to avoid be called during one specific test. But it always got executed.

Im doing this.

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

the commit method continue executing the original send_to method.

EDIT: YES, ISSUE IS BECAUSE I WAS STUBING SENT_TO AND IT SHOULD BE SEND_TO.

Upvotes: 0

Views: 42

Answers (1)

Ariejan
Ariejan

Reputation: 11069

Two comments on your code.

  1. As @fivedigit says, you stub sent_to, but claim the method is named send_to.
  2. Your spec description is difficult to read, maybe some thing like "raises an error when no jobs have the right state" or "raises an error when all jobs have the wrong state".

Upvotes: 1

Related Questions