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

SpringChicken anonymousleggingsgenie at gmail.com
Thu Jan 13 16:53:33 EST 2011

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 }
=> 'params'})
          put :update, :id => "14", :post => {'these' => 'params'}

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 }
        format.html { render :action => "edit" }
        format.xml  { render :xml => @comment.errors, :status
=> :unprocessable_entity }


More information about the rspec-users mailing list