samedi 26 décembre 2015

Rails Testing - Expected response to be a

I am trying to better understand the server log to enhance my testing environment. For most of the query I send to the server I (almost) always get a 200 message as follows:

Completed 200 OK in 410ms (Views: 403.0ms | ActiveRecord: 1.7ms)

This (seems to) trigger(s) the following failure in my testing when using the assert_redirected_to method:

Expected response to be a <redirect>, but was <200>

For example, If I update my model "Users" as follows in my controller

def update
  @user = User.find(params[:id])
  if @user.update_attributes(user_params)
  flash[:success] = "Your profil has been updated"
  redirect_to @user
  end
end

I want to test the redirection. I use:

test "friendly forwarding" do
   get edit_user_path(@user) #user is a very basic fixture that I call in the test
   log_in_as(@user)
   assert_redirected_to edit_user_path(@user)
   patch user_path(@user), user: { name:  "Foo Bar", email: "foo@bar.com }
   assert_redirected_to @user #line that makes the test fails
end

What is wrong? Should I use something different than assert_redirected_to method or do I have an issue with my code which should not send back a 200 message?

Aucun commentaire:

Enregistrer un commentaire