在 Rails 教程第 8 章中遇到错误
Experiencing errors in The Rails Tutorial Chapter 8
我尝试修复它但无济于事。我很确定我遵循了教程。这发生在 Rails 教程的第 8 章。
这里是我运行bundle exec rake test
时的错误
ERROR["test_current_user_returns_nil_when_remember_digest_is_wrong", SessionsHelperTest, 2015-09-17 16:33:02 +0100]
test_current_user_returns_nil_when_remember_digest_is_wrong#SessionsHelperTest (1442503982.53s)
NoMethodError: NoMethodError: undefined method `user' for #<SessionsHelperTest:0xbdef3ba8>
test/helpers/sessions_helper_test.rb:6:in `setup'
test/helpers/sessions_helper_test.rb:6:in `setup'
ERROR["test_current_user_returns_right_user_when_session_is_nil", SessionsHelperTest, 2015-09-17 16:33:02 +0100]
test_current_user_returns_right_user_when_session_is_nil#SessionsHelperTest (1442503982.55s)
NoMethodError: NoMethodError: undefined method `user' for #<SessionsHelperTest:0xb9575274>
test/helpers/sessions_helper_test.rb:6:in `setup'
test/helpers/sessions_helper_test.rb:6:in `setup'
ERROR["test_login_with_remembering", UsersLoginTest, 2015-09-17 16:33:02 +0100]
test_login_with_remembering#UsersLoginTest (1442503982.61s)
NoMethodError: NoMethodError: undefined method `session' for nil:NilClass
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:51:in `block in <class:UsersLoginTest>'
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:51:in `block in <class:UsersLoginTest>'
ERROR["test_login_without_remembering", UsersLoginTest, 2015-09-17 16:33:02 +0100]
test_login_without_remembering#UsersLoginTest (1442503982.73s)
NoMethodError: NoMethodError: undefined method `session' for nil:NilClass
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:56:in `block in <class:UsersLoginTest>'
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:56:in `block in <class:UsersLoginTest>'
28/28: [=======================================================================================================] 100% Time: 00:00:03, Time: 00:00:03
Finished in 3.29948s
28 tests, 62 assertions, 0 failures, 4 errors, 0 skips
test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end
# Logs in a test user.
def log_in_as(user, options = {})
password = options[:password] || 'password'
remember_me = options[:remember_me] || '1'
if integration_test?
post login_path, session: { email: user.email,
password: password,
remember_me: remember_me }
else
session[:user_id] = user.user_id # this is line 27
end
end
private
# Returns true inside an integration test.
def integration_test?
defined?(post_via_reditect)
end
end
test/helpers/sessions_helper.rb
require 'test_helper'
class SessionsHelperTest < ActionView::TestCase
def setup
@user = user(:microte) # Line 6
remember(@user)
end
test "current_user returns right user when session is nil" do
assert_equal @user, current_user
assert is_logged_in?
end
test "current_user returns nil when remember digest is wrong" do
@user.update_attribute(:remember_digest, User.digest(User.new_token))
assert_nil current_user
end
end
test/integration/users_login_test.rb
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:microte)
end
test "login with invalid information" do
get login_path
assert_template 'sessions/new'
post login_path session: { email: "", password: "" }
assert_template 'sessions/new'
assert_not flash.empty?
get root_path
assert flash.empty?
end
test "login with valid information" do
get login_path
post login_path, session: { email: @user.email, password: 'password' }
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
end
test "login with valid information follwed by logout" do
get login_path
post login_path, session: { email: @user.email, password: 'password' }
assert is_logged_in?
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
delete logout_path
assert_not is_logged_in?
assert_redirected_to root_url
# Simulate a user clicking logout in a second window
delete logout_path
follow_redirect!
assert_select "a[href=?]", login_path
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end
test "login with remembering" do
log_in_as(@user, remember_me: '1') # Line 51
assert_not_nil cookies['remember_token']
end
test "login without remembering" do
log_in_as(@user, remember_me: '0') # Line 56
assert_nil cookies['remember_token']
end
end
感谢您的帮助。
似乎您在尝试从固定装置中拉出 user
时遇到错误。检查您的 fixtures/users.yml
文件,看看您是否定义了 microte
用户。它应该看起来像这样(只是一个例子):
microte:
name: "Test User"
email: "test_user@example.com"
password_digest: <%= User.digest("password")%>
slug: <%= "Test User".parameterize %>
activated: true
前两个错误似乎在您的 session_helper 中:您的设置定义了 @user = user(:microte)
。
不确定记忆测试中的错误是;您共享的测试似乎没问题。我建议您将代码更改与教程中的内容进行比较,看看这些错误的原因是什么。
在教程中,提供的用户名为:michael
正如我之前的其他人所指出的,您改为写了 :microte。
检查你的 users.yml 夹具你在那里如何命名他,并确保它们匹配。
作为一点解释:在您的测试开始时,有一个设置方法可以准备您的测试设置:
def setup
@user = users(:microte)
end
您在此处实例化的用户已放入 fixtures 文件中。
我尝试修复它但无济于事。我很确定我遵循了教程。这发生在 Rails 教程的第 8 章。
这里是我运行bundle exec rake test
ERROR["test_current_user_returns_nil_when_remember_digest_is_wrong", SessionsHelperTest, 2015-09-17 16:33:02 +0100]
test_current_user_returns_nil_when_remember_digest_is_wrong#SessionsHelperTest (1442503982.53s)
NoMethodError: NoMethodError: undefined method `user' for #<SessionsHelperTest:0xbdef3ba8>
test/helpers/sessions_helper_test.rb:6:in `setup'
test/helpers/sessions_helper_test.rb:6:in `setup'
ERROR["test_current_user_returns_right_user_when_session_is_nil", SessionsHelperTest, 2015-09-17 16:33:02 +0100]
test_current_user_returns_right_user_when_session_is_nil#SessionsHelperTest (1442503982.55s)
NoMethodError: NoMethodError: undefined method `user' for #<SessionsHelperTest:0xb9575274>
test/helpers/sessions_helper_test.rb:6:in `setup'
test/helpers/sessions_helper_test.rb:6:in `setup'
ERROR["test_login_with_remembering", UsersLoginTest, 2015-09-17 16:33:02 +0100]
test_login_with_remembering#UsersLoginTest (1442503982.61s)
NoMethodError: NoMethodError: undefined method `session' for nil:NilClass
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:51:in `block in <class:UsersLoginTest>'
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:51:in `block in <class:UsersLoginTest>'
ERROR["test_login_without_remembering", UsersLoginTest, 2015-09-17 16:33:02 +0100]
test_login_without_remembering#UsersLoginTest (1442503982.73s)
NoMethodError: NoMethodError: undefined method `session' for nil:NilClass
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:56:in `block in <class:UsersLoginTest>'
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:56:in `block in <class:UsersLoginTest>'
28/28: [=======================================================================================================] 100% Time: 00:00:03, Time: 00:00:03
Finished in 3.29948s
28 tests, 62 assertions, 0 failures, 4 errors, 0 skips
test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end
# Logs in a test user.
def log_in_as(user, options = {})
password = options[:password] || 'password'
remember_me = options[:remember_me] || '1'
if integration_test?
post login_path, session: { email: user.email,
password: password,
remember_me: remember_me }
else
session[:user_id] = user.user_id # this is line 27
end
end
private
# Returns true inside an integration test.
def integration_test?
defined?(post_via_reditect)
end
end
test/helpers/sessions_helper.rb
require 'test_helper'
class SessionsHelperTest < ActionView::TestCase
def setup
@user = user(:microte) # Line 6
remember(@user)
end
test "current_user returns right user when session is nil" do
assert_equal @user, current_user
assert is_logged_in?
end
test "current_user returns nil when remember digest is wrong" do
@user.update_attribute(:remember_digest, User.digest(User.new_token))
assert_nil current_user
end
end
test/integration/users_login_test.rb
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:microte)
end
test "login with invalid information" do
get login_path
assert_template 'sessions/new'
post login_path session: { email: "", password: "" }
assert_template 'sessions/new'
assert_not flash.empty?
get root_path
assert flash.empty?
end
test "login with valid information" do
get login_path
post login_path, session: { email: @user.email, password: 'password' }
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
end
test "login with valid information follwed by logout" do
get login_path
post login_path, session: { email: @user.email, password: 'password' }
assert is_logged_in?
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
delete logout_path
assert_not is_logged_in?
assert_redirected_to root_url
# Simulate a user clicking logout in a second window
delete logout_path
follow_redirect!
assert_select "a[href=?]", login_path
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end
test "login with remembering" do
log_in_as(@user, remember_me: '1') # Line 51
assert_not_nil cookies['remember_token']
end
test "login without remembering" do
log_in_as(@user, remember_me: '0') # Line 56
assert_nil cookies['remember_token']
end
end
感谢您的帮助。
似乎您在尝试从固定装置中拉出 user
时遇到错误。检查您的 fixtures/users.yml
文件,看看您是否定义了 microte
用户。它应该看起来像这样(只是一个例子):
microte:
name: "Test User"
email: "test_user@example.com"
password_digest: <%= User.digest("password")%>
slug: <%= "Test User".parameterize %>
activated: true
前两个错误似乎在您的 session_helper 中:您的设置定义了 @user = user(:microte)
。
不确定记忆测试中的错误是;您共享的测试似乎没问题。我建议您将代码更改与教程中的内容进行比较,看看这些错误的原因是什么。
在教程中,提供的用户名为:michael 正如我之前的其他人所指出的,您改为写了 :microte。 检查你的 users.yml 夹具你在那里如何命名他,并确保它们匹配。
作为一点解释:在您的测试开始时,有一个设置方法可以准备您的测试设置:
def setup
@user = users(:microte)
end
您在此处实例化的用户已放入 fixtures 文件中。