简体   繁体   中英

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

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?

 def update @user = User.find(params[:id]) if @user.update_attributes(user_params) flash[:success] = "Your profil has been updated" redirect_to @user end # right here is where the 200 comes in. end 

You are rendering the partial when the update fails, thus the response of 200. Generally the update method will display the validation errors and render the edit form at this point. See here: Successful and unsuccessful updates from the rails tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM