Rails + Rspec: How to stub a record so that it's marked as invalid? -


i have purchase model method:

def set_status_to_in_progress!   self.update_attributes!(status: in_progress) end 

and failing rspec test:

context "self invalid"   "raises error"     purchase = purchase.new     purchase.stub(:valid?).and_return(:false)     expect { purchase.set_status_to_in_progress! }.to raise_error   end end 

which returns

failures:        1) purchase#set_status_to_in_progress! self invalid raises error          failure/error: expect { purchase.set_status_to_in_progress! }.to raise_error            expected exception nothing raised          # ./spec/models/purchase_spec.rb:149:in `block (4 levels) in <top (required)>' 

i thought stubbing valid? enough make activerecord update_attributes! method raise error? how make raise?

try changing :false false

purchase.stub(:valid?).and_return(false) 

or

purchase.should_receive(:valid?).and_return(false) 

otherwise can stub instance of purchase

purchase.any_instance.should_receive(:valid?).and_return(false) 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -