[rspec-users] Route is valid but not found :(

ct9a anexiole at gmail.com
Mon Aug 22 23:09:23 EDT 2011



Thanks, Justin.

I have the part object mocked up before each spec runs.

------- Extract begins -----------------

    let(:part){
        mock_model('Part').as_null_object
    }

    before do
        Part.stub(:new).and_return(part)
    end

------- Extract ends -----------------


With that in mind, I just made changes to my spec as below:

------- Extract begins -----------------

    it 'saves updates to an existing part object successfully' do
        Part.should_receive(:find).with(1).and_return(part)
        Part.should_receive(:update).with( 'title' =>
'Grimspeed' ).and_return(part)
        put :update, :id => 1, :part => { 'title' => 'Grimspeed' }
    end

------- Extract ends -----------------


When I run the 'rake spec:controllers', I get the following error:


------- Error extract begins -----------------

  1) PartsController saves updates to an existing part object
successfully
     Failure/Error: Part.should_receive(:update).with( 'title' =>
'Brake pads' ).and_return(part)
       (<Part(id: integer, title: string, description: text,
created_by: string, updated_by: string, created_at: datetime,
updated_at: datetime) (class)>).update({"title"=>"Brake pads"})
           expected: 1 time
           received: 0 times
     # ./spec/controllers/parts_controller_spec.rb:53

------- Error extract ends -----------------

I then put in Justin's change with the use of double() as follows:

------- Source code extract begins -----------------

    it 'saves updates to an existing part object successfully' do
        part = double(part)
        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

------- Source code extract ends -----------------

and I got the following error:

------- Error extract begins -----------------

  1) PartsController saves updates to an existing part object
successfully
     Failure/Error: put :update, :id => 1, :part => { 'title' =>
'Brake pads' }
       Double received unexpected message :update_attributes with
({"title"=>"Brake pads"})
     # ./app/controllers/parts_controller.rb:63:in `update'
     # ./app/controllers/parts_controller.rb:62:in `update'
     # ./spec/controllers/parts_controller_spec.rb:55

------- Error extract ends -----------------

I have also looked at a few other posts in the group but can't figure
out what's causing
update_attributes not to be recognised :(

- http://groups.google.com/group/rspec/browse_thread/thread/9bfb4a348799c30f/30de6b7953222b5a?lnk=gst&q=update_attributes#30de6b7953222b5a
- http://groups.google.com/group/rspec/browse_thread/thread/fe4985e2b8be16da/f590a9e0510ff4fb?lnk=gst&q=update_attributes#f590a9e0510ff4fb

My update message in the parts controller reads as below:

------- Source code extract starts -----------------
  def update
    @part = Part.find(params[:id])

    respond_to do |format|
      if @part.update_attributes(params[:part])
        format.html { redirect_to(@part, :notice => 'Part was
successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @part.errors, :status
=> :unprocessable_entity }
      end
    end
  end

------- Source code extract ends -----------------


I have used this bit of code without problems in the past.
update_attributes, like save, is part of the ActiveRecord api (http://
ar.rubyonrails.org/).

Well, given that controllers are not to know of the implementation of
messages,
I have then stubbed the message to the update_attributes method in the
parts controller.

------- 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 -----------------

Alas, I got the error below :(

------- Error extract starts -----------------

  1) PartsController saves updates to an existing part object
successfully does its job in saving the update
     Failure/Error: Part.should_receive(:update).with('title' =>
'Brake pads').and_return(part)
       (<Part(id: integer, title: string, description: text,
created_by: string, updated_by: string, created_at: datetime,
updated_at: datetime) (class)>).update({"title"=>"Brake pads"})
           expected: 1 time
           received: 0 times
     # ./spec/controllers/parts_controller_spec.rb:61

------- Error extract ends -----------------


More information about the rspec-users mailing list