I have the following test: test/integration/authentication_test.rb:
require 'test_helper'
class AuthenticationTest < ActionDispatch::IntegrationTest
def setup
@admin = users(:barry) # grab user from fixtures
end
test "trying to view a user before logging in" do
get user_path(@admin)
assert_template 'sessions/new'
assert_not flash.empty?
assert_select "div#error_explanation"
assert_select "div.field_with_errors"
assert_select "a[href=?]", logout_path, count: 0
assert_not is_logged_in?
end
end
The test fails with the following error:
FAIL["test_trying_to_view_a_user_before_logging_in", AuthenticationTest, 2.206536] test_trying_to_view_a_user_before_logging_in#AuthenticationTest (2.21s)
expecting <"sessions/new"> but rendering with <[]>
test/integration/authentication_test.rb:11:in `block in <class:AuthenticationTest>'
Relevant bits of the users_controller.rb:
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :show, :edit, :update, :destroy]
def show
@user = User.find_by_callsign(params[:callsign])
@page_name = "user_page"
redirect_to root_url and return unless @user.activated
end
.
.
end
sessions_helper.rb:
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
def logged_in?
!current_user.nil?
end
In routes.rb:
get 'login', to: 'sessions#new'
I don't understand why the test is failing. When I perform the steps manually it all works. Is there a known issue with assert_template? When I comment out assert_template 'sessions/new'
in the test, it passes.
Aucun commentaire:
Enregistrer un commentaire