On Sep 3, 1:06 am, Pallavi Naik <[email protected]> wrote:
> Hi,
>
> As per the homework we are required to update the title and add a comment to
> the first post and also assert that these changes have been done.
> Accordingly I have written the following code that updates the title of the
> first post and asserts that the update has been done:
>
> *post* 'posts/update', :id => posts(:one).id, :post => {:title => "New
> Title"}
> assert_equal 'Post was successfully updated.' , flash[:notice]
> assert_redirected_to post_path(assigns(:post))
>
> However when I change the first line as follows the test fails:
>
> *put* 'posts/update', :id => posts(:one).id, :post => {:title => "New
> Title"}
> assert_equal 'Post was successfully updated.' , flash[:notice]
> assert_redirected_to post_path(assigns(:post))
>
> I would like to know why the put method does not work here since we are
> supposed to use the put method while doing updates.
With post, you feed the request with data, and let the system handle
the data. So that you need a generic uri, an id, and data, and then
all of that is combined to update the resource.
With put, you get to an exact uri, and give it the exact data, then it
handles exactly what you give. So that in the uri of the put you need
the id.
See here an excellent explanation of the difference between post and
put:
http://stackoverflow.com/questions/107390/whats-the-difference-between-a-post-and-a-put-http-request
>
> I also don't understand how the assert_difference works in the below code
> and why is it required??
>
> assert_difference('Post.count') do
> post :create, :post => {:title => "my really new post", :body =>"my
> really new body" }
> end
Here you simulate the create of a post, and normally if it is created,
the number of rows in the database table should change, this is why
you test that the count (number of rows) is different after creating
the post. If the count is the same, that means that the creation has
failed.
>
> assert_difference('Post.count', -1) do
> delete :destroy, :id => posts(:one).id
> end
Here this is the same with the delete method. If you delete a post,
then the number of rows in the table should decrement, hence you test
if this is true. If not, that means that the deletion has failed.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "ruby-on-rails-programming-with-passion" group.
To unsubscribe from this group, send email to
ruby-on-rails-programming-with-passion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ruby-on-rails-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---