On 23 August 2011 22:19, Ken Chien <[email protected]> wrote:
> I think you were closer when you had:
>
> ------- Source code extract starts -----------------
> context 'saves updates to an existing part object successfully' do
> before do
> part =
> double('part').stub(:update_
> attributes).and_return(true)
> end
>
> it 'does its job in saving the update' do
> Part.should_receive(:find).with(1).and_return(part)
> Part.should_receive(:update).with('title' => 'Brake
> pads').and_return(part)
> put :update, :id => 1, :part => {'title' => 'Brake pads'}
> end
> end
> ------- Source code extract ends -----------------
>
> However, you were setting part equal to the return value from
> "stub(:update_attributes).and_return(true)" which returns a Proc, not a
> double.
> Instead, try this:
>
> let(:part) { double('part') }
>
>
> it 'does its job in saving the update' do
> part.stub(:update_attributes).and_return(true) # <--- moved this
> from the before method
>
>
> Part.should_receive(:find).with(1).and_return(part)
> Part.should_receive(:update).with('title' => 'Brake
> pads').and_return(part)
> put :update, :id => 1, :part => {'title' => 'Brake pads'}
> end
> end
>
Hello, Ken,
Thanks for that.
Makes sense.
As I have a 'let' on the top of my specs script, and that the other specs
(for new objects) actually use 'mock_model('Part').as_null_object', I put
the code below in the criteria for
'saves updates to an existing part object successfully' within a before
block.
Spec passes as expected.
----------------- 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.stub(: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 --------------------------------
Now, I'm going to try out David Chelimsky's recommendations cause he's also
right - i should have used should_receive on the part object and NOT the
class :)
--
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.