The expected response will be <redirect>, but was <200>

I am trying to test the create action in a UserController where the user is created with a profile; then the user is redirected to the user profile page, but I get an error Expected response to be a <redirect>, but was <200>.

CREATE ACTION IN THE USER CONTROLLER

def create
  @user = User.new(user_params)
  if @user.save
    log_in @user
    flash[:success] = "Welcome to the Mini Olympics"
    redirect_to user_profile_path(current_user, @profile)
  else
    render 'new'
  end
end

SIGNUP TEST USING REAL INFORMATION

test "valid signup information" do
  get signup_path
  assert_difference 'User.count', 1 do
    post users_path, user: @user.attributes
    @user.build_profile(name: "Micheal Example", street: "24 Martins St.", city: "Waterloo", state: "AW", zipcode: "22456")
    @user.save
    #assert_redirected_to user_profile_path(@user)
    assert_redirected_to @user.profile
  end
  assert_template 'profiles/show'
  assert is_logged_in?
end
+4
source share
3 answers

The error message means that the create action does not work, and instead of redirecting to the user profile (status 302 ), it displays the form again (status 200 ) ..

, , . , , @user @user , .

- params, :

post users_path, user: @user.attributes

@user, . , @user (, ) .

, - :

 assert_difference 'User.count', 1 do
   user = User.new
   user.build_profile(name: "Micheal Example", street: "24 Martins St.", city: "Waterloo", state: "AW", zipcode: "22456")
   post users_path, user: user.attributes
   assert_redirected_to user.profile
 end

user . post.

+10

, , , . , , , - . user.rb :

    validates :password, length: { minimum: 8 }

6 , . , -, , .

+1

in each controller file name in the test file def setup @user = users (: user) log_in_as (@user) end

-1
source

Source: https://habr.com/ru/post/1605857/


All Articles