Reputation: 361
Going through Hartl's tutorial and have searched for an answer, but found none. (There may be answers existing but becuase the chapters have been updated and switched around in the newer editions I can't find them if they exist.)
I am at section 10.2.2. listing 10.26 doing the rails test.
here is the error for my tests:
ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 1.5802158990409225]
test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1.58s)
BCrypt::Errors::InvalidHash: BCrypt::Errors::InvalidHash: invalid hash
app/controllers/sessions_controller.rb:9:in `create'
test/test_helper.rb:32:in `log_in_as'
test/controllers/users_controller_test.rb:29:in `block in <class:UsersControllerTest>'
ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 1.606778411893174]
test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1.61s)
BCrypt::Errors::InvalidHash: BCrypt::Errors::InvalidHash: invalid hash
app/controllers/sessions_controller.rb:9:in `create'
test/test_helper.rb:32:in `log_in_as'
test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
35/35: [=====================================================] 100% Time: 00:00:01, Time: 00:00:01
Finished in 1.71823s
35 tests, 87 assertions, 0 failures, 2 errors, 0 skips
test/fixtures/users.yml
michael:
name: Michael Example
email: [email protected]
password_digest: <%= User.digest('password') %>
archer:
name: Sterling Archer
email: [email protected]
password_digest: <%= User.digest('password') %
test/controllers/users_controller_test.rb
require 'test_helper'
class UsersControllerTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
@other_user = users(:archer)
end
test "should get new" do
get signup_path
assert_response :success
end
test "should redirect edit when not logged in" do
get edit_user_path(@user)
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect update when not logged in" do
patch user_path(@user), params: { 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_user_path(@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 user_path(@user), params: { user: { name: @user.name,
email: @user.email } }
assert flash.empty?
assert_redirected_to root_url
end
end
app/controllers/users_controller.rb
class UsersController < ApplicationController
before_action :logged_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
# @user = User.new(params[:user]) # Not the final implementation!
if @user.save
log_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
# Handle a successful save.
else
render 'new'
end
end
def edit
# can take out because already in correct_user @user = User.find(params[:id])
end
def update
# can take out because already in correct_user @user = User.find(params[:id])
if @user.update_attributes(user_params)
# Handle a successful update.
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless @user == current_user
end
end
Why am I getting 2 failed tests when the book says that all tests should pass? I have looked at the lines the error message mentions ,but I am not sure what is wrong. Thanks for the help.
EDIT: Sessions controller
class SessionsController < ApplicationController
def new
end
def create
@user = User.find_by(email: params[:session][:email].downcase)
if @user && @user.authenticate(params[:session][:password])
log_in @user
params[:session][:remember_me] == '1' ? remember(@user) : forget(@user)
redirect_to @user
else
flash.now[:danger] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
Upvotes: 0
Views: 219
Reputation: 40
You're missing a closing angle bracket in the password_digest
for the "Sterling Archer" test fixture.
password_digest: <%= User.digest('password') %
should be
password_digest: <%= User.digest('password') %>
Upvotes: 1
Reputation: 83
look more attentively at line:
BCrypt::Errors::InvalidHash: invalid hash
app/controllers/sessions_controller.rb:9:in `create'
You did not specify the code for this controller, and I can not tell you what might be wrong.
Upvotes: 0