On chapter 9.2 of Hartl's Ruby on Rails Tutorial, I'm getting this 2 errors on the rake test after i made the test for redirecting user when they try to edit or update a profile that isn't theirs :
ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 2016-04-18 06:27:31 +0100]
test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1460957251.39s)
NoMethodError: NoMethodError: undefined method `user_id' for #<User:0x00564114110958>
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'
ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 2016-04-18 06:27:31 +0100]
test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1460957251.40s)
NoMethodError: NoMethodError: undefined method `user_id' for #<User:0x00564114035628>
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
The errors messages points to line 24 in my test_helper and line 28,36 in my users_controller_test :
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
test_helper.rb :
Line 18 would be at "remember_me" in the "log_in_as" method.
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
# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end
# Log 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
end
end
# Returns true inside an integration test.
def integration_test?
defined?(post_via_redirect)
end
# Add more helper methods to be used by all tests here...
end
user_controller_test.rb :
Line 28 would be at "log_in_as(@other_user)" in test "should redirect update when not logged in" and 36 at "log_in_as(@other_user)" in test "should redirect update when logged in as wrong user".
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
def setup
@user = users(:michael)
@other_user = users(:archer)
end
test "should get new" do
get :new
assert_response :success
end
test "should redirect edit when not logged in" do
get :edit, id: @user
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect update when not logged in" do
patch :update, id: @user, user: { name: @user.name, email: @user.email }
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect edit when logged in as wrong user" do
log_in_as(@other_user)
get :edit, id: @user
assert flash.empty?
assert_redirected_to root_url
end
test "should redirect update when logged in as wrong user" do
log_in_as(@other_user)
patch :update, id: @user, user: { name: @user.name, email: @user.email }
assert flash.empty?
assert_redirected_to root_url
end
end
and this is the fixture file where :archer and :michael are stored(test/fixtures/users.yml):
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
archer:
name: Sterling Archer
email: duchess@example.gov
password_digest: <%= User.digest('password') %>
else
session[:user_id] = user.user_id
end
There's no such attribute as user_id
, you want...
else
session[:user_id] = user.id
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.