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
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
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.
In log/test.log: it is indeed redirecting to the correct template (Redirected tohttp://www.example.com/dominos/newname). But it doesn’t have any ‘rendered’ lines. The last few lines of the failed test are:
Redirected to http://www.example.com/dominos/newname Completed 302 Found in 21ms (ActiveRecord: 2.5ms) [1m[35m (0.4ms)[0m SELECT COUNT(*) FROM "personas" [1m[36m (0.2ms)[0m [1mROLLBACK[0m
In the test.log file for successful tests involving assert_template, there are various ‘Rendered’ lines following a redirect, for example:
Rendered personas/new.html.erb within layouts/application (2.0ms)
Is this perhaps part of why the test is failing? Why does the page not render?