On Aug 23, 2011, at 6:51 PM, Gordon Yeong wrote: > On 23 August 2011 22:46, David Chelimsky <[email protected]> wrote: > > ^^ Change the class name Part to the variable part ^^: > > part.should_receive(:update_attributes).with('title' => 'Brake > pads').and_return(part) > > It's the part object, not the Part class that will receive update_attributes. > > HTH, > David > > Hi, David, > Thanks for that. Makes sense that the update_attributes message is to be > received by the instantiated object and not the class. > > I made the following change and yet, I get an error implying that the > update_attributes message is never called. > > > ----------------- Spec extract starts -------------------------------- > > context 'saves updates to an existing part object successfully' do > before do > part = double('part') > end > it 'does its job in saving the update' do > part.should_receive(:update_attributes).and_return(true) > Part.should_receive(:find).with(1).and_return(part) > put :update, :id => 1, :part => {'title' => 'Brake pads'} > flash[:notice].should eq('Part was successfully updated.') > end > end > > ----------------- Spec extract ends -------------------------------- > > and the error reads: > > > ------- Error extract begins ----------------- > > 1) PartsController saves updates to an existing part object successfully > does its job in saving the update > Failure/Error: part.should_receive(:update_attributes).and_return(true) > (Mock "Part_1006").update_attributes(any args) > expected: 1 time > received: 0 times > # ./spec/controllers/parts_controller_spec.rb:56 > > Finished in 0.25365 seconds > 8 examples, 1 failure, 1 pending > > > ------- Error extract ends -----------------
This is the downside of the tradeoff of mocking AR APIs - there are many to choose from, and they are used somewhat interchangeably. From a purist mock objects perspective, this is not the sort of thing you'd want to be mocking (it's Rails' APIs, not your own) because it's somewhat brittle. The upsides are that the examples run faster and broken models won't cause controller spec failures. Cheers, David -- You received this message because you are subscribed to the Google Groups "rspec" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rspec?hl=en.
