我正在阅读关于如何使用表单测试提交的第 7 章,书籍 <The ruby on rails tutorial 6th 2021>,下面有两个测试用例,令人困惑的部分是关于何时应该使用 follow_redirect!为什么第一种情况不需要这条线,而第二种需要呢?我的假设是关于 URL 本身,因为在第一种情况下,URL 一直是“用户/新”,但第二个后来变成“用户/显示”(在成功注册新用户后)。
非常感谢你的帮助。
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: 'user@example.com',
password: 'password',
password_confirmation: 'password' } }
end
follow_redirect! #It's all about This line!
assert_template 'users/show'
end
end