我试过修理但无济于事。我很确定我遵循了教程。这发生在The Rails Tutorial的第8章中。
运行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
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_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
测试/助手/ sessions_helper.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
测试/集成/ users_login_test.rb
{{1}}
感谢您的帮助。
答案 0 :(得分:0)
似乎在尝试从你的灯具中取出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
答案 1 :(得分:0)
看起来你的session_helper中出现了前两个错误:你的设置定义了@user = user(:microte)
。
不确定记忆测试中的错误是什么;您分享的测试似乎没问题。我建议你将代码更改与教程中的内容进行比较,看看这些错误的原因是什么。