I am reading the 7th chapter about how to test submission with the form with book <The ruby on rails tutorial 6th 2021>, and there are two test cases below, and the confusing part is about when should I use follow_redirect! and why the first case doesn't need this line, and the second one needs it? my assumption is about the URL itself because in the first case the URL is 'users/new' all the time, but the second one becomes 'users/show' later(after successful signup a new User).
Thank you so much for helping.
require 'test_helper'
class UsersSignupTest < ActionDispatch::IntegrationTest
test 'invalid signup information' do
get signup_path
assert_no_difference 'User.count' do
post users_path, params: { user: { name: '',
email: 'user@invalid',
password: 'foo',
password_confirmation: 'bar' } }
end
assert_template 'users/new'
assert_select 'div#error_explanation'
assert_select 'div.alert.alert-danger'
end
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! #It's all about This line!
assert_template 'users/show'
end
end
Suddenly find this part code in the users_controller.rb file, I guess maybe this is the reason, because when unsuccessfully submit, I just render the 'new' page, but when successfully submit, I redirect_to @user!
Really hope someone can give any confirm, thanks a lot.