Forum: RSpec what does 'these' mean in a PUT controller spec?

Posted by Gordon (Guest)
on 2012-01-16 12:13
(Received via mailing list)
hi, all,

  the spec above is found in my controller spec for a 'category'
resource.

  It's a generated spec.

----- start extract --------------------------------------------
  describe "PUT update" do
    describe "with valid params" do
      it "updates the requested category" do
        category = Category.create! valid_attributes
        # Assuming there are no other categories in the database, this
        # specifies that the Category created on the previous line
        # receives the :update_attributes message with whatever params
are
        # submitted in the request.

Category.any_instance.should_receive(:update_attributes).with({'these'
=> 'params'})
        put :update, :id => category.id, :category => {'these' =>
'params'}
      end
----- end extract --------------------------------------------

When the spec is run, it fails with the error below.

----- start extract --------------------------------------------

  1) CategoriesController PUT update with valid params updates the
requested category
     Failure/Error: put :update, :id => category.id, :category =>
{'these' => 'params'}
       #<Category:0x000001017cd498> received :update_attributes with
unexpected arguments
         expected: ({"these"=>"params"})
              got: ({"these"=>"params", "updated_by"=>1})
     # /Users/anexiole/projects/try_rails/app/controllers/
categories_controller.rb:72:in `block in update'
     # /Users/anexiole/projects/try_rails/app/controllers/
categories_controller.rb:71:in `update'
     # ./categories_controller_spec.rb:92:in `block (4 levels) in <top
(required)>'

Finished in 19.15 seconds
16 examples, 1 failure

Failed examples:

rspec ./categories_controller_spec.rb:85 # CategoriesController PUT
update with valid params updates the requested category

----- end extract --------------------------------------------

  My 'update' method in the categories controller file itself has one
added rule for which
I will assign the current user's id  to the updated_by attribute
before a call to update_attribute is made.

----- start extract --------------------------------------------
  # PUT /categories/1
  # PUT /categories/1.json
  def update
    # Record current user's id as he/she created the part
    params[:category][:updated_by] = current_user.id

    @category = Category.find(params[:id])

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

----- end extract --------------------------------------------


Can someone please tell me what does 'these' refer to in the spec?
Where can I read up more about them?
I would like to fix the failing spec example.

Thank you

 Gorodn
Posted by David Chelimsky (Guest)
on 2012-01-16 15:00
(Received via mailing list)
On Jan 16, 2012, at 4:59 AM, Gordon wrote:

>      it "updates the requested category" do
> 'params'}
> {'these' => 'params'}
>
>  My 'update' method in the categories controller file itself has one
>
> status: :unprocessable_entity }
>      end
>    end
>  end
>
> ----- end extract --------------------------------------------
>
>
> Can someone please tell me what does 'these' refer to in the spec?
> Where can I read up more about them?
> I would like to fix the failing spec example.

What's being specified here is that the contents of params[:category] 
are passed to update attributes. What they actually contain is not of 
concern to the controller or the controller spec, since they get passed 
directly to the model in the generated controller. 'these' => 'params' 
could just as easily be 'foo' => 'bar'. The important thing is that the 
same params get received by the model object.

The reason the spec is failing now is that the line you added modifies 
the hash before it gets sent to model object. If you change the spec to 
this:

  Category.any_instance.should_receive(:update_attributes).with('these' 
=> 'params', 'updated_by' => 1)

then it will pass. If you find 'these' => 'params' to be confusing, feel 
free to change it to what ever you like. Just make sure you do so in 
both places in the example.

HTH,
David
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.