Been stuck on a test error on Ruby on Rails tutorial- Error: UsersControllerTest#test_should_redirect_update_when_logged_in_as_wrong_user: -
i've been stuck on 1 while , keep getting 2 errors when rake test. referencing listing 9.23 testing point. appreciated.
test error:
1) error: userscontrollertest#test_should_redirect_update_when_logged_in_as_wrong_user: nameerror: undefined local variable or method `user_id' #<userscontrollertest:0x007fbff6be3120> test/test_helper.rb:24:in `log_in_as' test/controllers/users_controller_test.rb:35:in `block in <class:userscontrollertest>' 2) error: userscontrollertest#test_should_redirect_edit_when_logged_in_as_wrong_user: nameerror: undefined local variable or method `user_id' #<userscontrollertest:0x007fbff6c01850> test/test_helper.rb:24:in `log_in_as' test/controllers/users_controller_test.rb:28:in `block in <class:userscontrollertest>' 29 runs, 66 assertions, 0 failures, 2 errors, 0 skips
user_controller_test file:
require 'test_helper' class userscontrollertest < actioncontroller::testcase def setup @user = users(:michael) @other_user = users(:archer) end test "should new" :new assert_response :success end test "should redirect edit when not logged in" :edit, id: @user assert_not flash.empty? assert_redirected_to login_url end test "should redirect update when not logged in" 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 wrong user" log_in_as(@other_user) :edit, id: @user assert flash.empty? assert_redirected_to root_url end
test_helper.rb:
env['rails_env'] ||= 'test' require file.expand_path('../../config/environment', __file__) require 'rails/test_help' class activesupport::testcase # setup fixtures in test/fixtures/*.yml tests in alphabetical order. fixtures :all def is_logged_in? !session[:user_id].nil? end #logs in 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_id end end private #returns true inside int. test def integration_test? defined?(post_via_redirect) end end
and user_id come here?
def log_in_as(user, options = {}) ... if integration_test? ... else session[:user_id] = user_id end end
seems should be:
session[:user_id] = user.id
Comments
Post a Comment