I am writing a test when creating a user registration
/sample_app/test/integration/users_signup_test.rb
require 'test_helper'
class UsersSignupTest < ActionDispatch::IntegrationTest
.
.
.
test "valid signup information" do
get signup_path
assert_difference 'User.count', 1 do
post users_path, params: { user: { name: "Example User",
email: "[email protected]",
password: "password",
password_confirmation: "password" } }
end
follow_redirect!
assert_template 'users/show'
end
What is follow_redirect!
!
This method looks at the result of sending a POST request and moves to the specified redirect destination. (Rails tutorial)
I can imagine the behavior somehow
What is the specified redirect destination?
If you run the test as it is
rails test
> Green
If you refer to the behavior of the controller corresponding to post users_path
/sample_app/app/controllers/users_controller.rb
.
.
.
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
If you change it to redirect_to @user
> redirect_to root_path
rails test
> Red
FAIL["test_valid_signup_information", #<Minitest::Reporters::Suite:0x000055e3f2c61b10 @name="UsersSignupTest">, 1.4677946789997804]
test_valid_signup_information#UsersSignupTest (1.47s)
expecting <"users/show"> but rendering with <["static_pages/home",
...
The redirect destination is "static_pages / home"
follow_redirect!
Is
"Look at the result of sending a POST request",
So it seems to follow the explicit redirect behavior in the corresponding controller
Recommended Posts