[rspec-users] Rspec Nested Resources: Can someone offer an 'update' example?

David Chelimsky dchelimsky at gmail.com
Mon Jan 17 14:47:49 EST 2011


On Jan 13, 2011, at 3:53 PM, SpringChicken wrote:

> Hi folks,
> 
> I've just spent a day trying to write up a basic update spec for a
> nested resource - all without avail. I'm quite new to RSpec & not sure
> what I'm doing wrong. I can't seem to get a stub that recognises the
> required association to the parent object. I've tried factories and
> outright database calls, none of which seem to catch the
> should_receive. If anyone could offer a basic example of the nested
> equivalent of something like the following, I'd be very appreciative.
> 
>        it "updates the requested post" do
>          Post.stub(:find).with("14") { mock_post }
>          mock_post.should_receive(:update_attributes).with({'these'
> => 'params'})
>          put :update, :id => "14", :post => {'these' => 'params'}
>        end
> 
> Controller action here:
> 
>  def update
>    @comment = Comment.find(params[:id])
>    respond_to do |format|
>      if @comment.update_attributes(params[:comment])
>        flash[:notice] = 'Post successfully updated'
>        format.html { redirect_to(@comment.post) }
>        format.xml  { head :ok }
>      else
>        format.html { render :action => "edit" }
>        format.xml  { render :xml => @comment.errors, :status
> => :unprocessable_entity }
>      end
>    end
> 
>  end

That looks like it should work. What's the failure message you're getting?


More information about the rspec-users mailing list