我有以下试验: 测试/集成/ authentication_test.rb:滑轨单元测试assert_template失败
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
的测试失败,错误如下:
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>'
的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
在routes.rb中:
get 'login', to: 'sessions#new'
我不明白为什么测试失败。当我手动执行这些步骤时,它一切正常。 assert_template是否存在已知问题?当我在测试中注释掉assert_template 'sessions/new'
时,它会通过。
编辑:
在日志/ test.log中:的确是重定向到正确的模板(重定向到http://www.example.com/dominos/newname)。但它没有任何“渲染”线。失败的测试的最后几行是:
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
在涉及assert_template成功测试test.log中的文件,也有不同的“渲染”下一个重定向行,例如:
Rendered personas/new.html.erb within layouts/application (2.0ms)
这可能是测试失败的原因吗?为什么页面不呈现?
运行此测试时,请查看'log/test.log'文件中的内容,并与手动执行步骤中的'log/development.log'文件内容进行比较。很可能测试正在采取不同的路线,并出现错误或其他问题。 –
谢谢。测试日志显示它确实重定向到正确的模板('重定向到http:// www.example.com/dominos/newname')。但它没有任何“渲染”线条,这可能是问题的一部分吗? (见编辑) – Bazley