Hello Rubyists,
I have this functional test which is failing, and I’m not sure why.
def test_update #Grab the post we want to update
get :edit, :id => @first_id
assert_response :success
assert_template ‘edit’
#Post the changes we want to make
post :update, :id => @first_id, :body => "New Body"
assert_response :redirect
assert_redirected_to :action => 'list'
#Verify that the changes were made successfully
assert_equal "Post was successfully updated.", flash[:notice]
post = BlogPost.find(@first_id)
#My test fails on this next line
assert_equal "New Body", post.body, "Expected bodies to match"
end
My “post :update” comes back successfully, but when I try and validate
that the post was actually updated, my test fails. The test fails
because the body wasn’t actually changed.
Does anyone have any insight they could share on this? Thanks in
advance!
I’m just guessing, based on what is typically done. Show use your
controller code and we’ll be able to give you a better answer.
Bob, that worked! Thank you very much. I’m still very new to programming
and Rails in general, so I still don’t fully understand how post, get,
etc. work. I guess I need to read up more on HTTP protocol.
#Post the changes we want to make
post :update, :id => @first_id, :body => "New Body"
What does your controller code look like? This would pass “New Body”
as params[:body]. If you’re using form_for() or similar, you probably
are doing something like this in your controller:
… @blog_post.attributes = params[:blog_post]
In which case you would need to pass the body like this in your test: