我正在阅读第7章关于如何使用带有书籍的表单测试提交
非常感谢您的帮助。
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
答案 0 :(得分:2)
突然在users_controller.rb文件中发现这部分代码,我猜可能是这个原因,因为提交不成功的时候,我只是渲染'new'页面,但是当成功提交时,我重定向到@user!
真的希望有人能给予确认,非常感谢。
def create
@user = User.new(user_params)
if @user.save
puts users_url
# puts root_url
puts user_url(@user)
flash[:success] = 'Welcome to the Sample App!'
redirect_to @user
# redirect_to root_path
else
render 'new'
end
end