Ruby on Rails测试与设计授权助手,在测试助手参数错误的数量



我有一个基本的Rails Scaffold,所有的控制器动作都由一个过滤器保护,该过滤器确定当前用户是否是管理员。而不是在admin &非管理员用户在每次控制器测试期间,我决定创建一个帮助器,用于登录用户并断言响应。所有的控制器测试工作正常,但我得到一个时髦的错误数量的参数错误在我的辅助函数(如下所示)。

控制器代码片段:

class QuotesController < ApplicationController
before_action :set_quote, only: [:show, :edit, :update, :destroy]
before_action :admin_filter
# GET /quotes
def index
@quotes = Quote.all
end
#... rest of actions...
private
# Returns true if user is admin
def is_admin?
current_user.boss if user_signed_in?
end
# Redirect If not Admin
def admin_filter
redirect_to root_path unless is_admin?
end
end

Test Helpers(减少登录和退出用户的冗余):

# test_helper.rb
class ActiveSupport::TestCase
include Devise::Test::IntegrationHelpers
# Add more helper methods to be used by all tests here...
module AuthTesting
# compacts admin_access and unsigned_no_admin to one test
def test_authorization(path, admin, non_admin) # Error @ this line, stacktrace ends 
admin_access(path, admin)
unsigned_no_admin_no_access(path, non_admin)
end
# asserts redirect for not signed in/ non admin users
def unsigned_no_admin_no_access(path, non_admin)
get path
assert_response :redirect
sign_in non_admin
get path
assert_response :redirect
sign_out non_admin
end
# Asserts that admin has access 
def boss_access(path, admin)
sign_in admin
get path
assert_response :success
sign_out admin # prevents test_access from having leftover signed in admin
end
end
end

测试片段(所有控制器动作正常工作&测试通过)

class QuotesControllerTest < ActionDispatch::IntegrationTest
include AuthTesting
setup do
@quote = quotes(:one)
@admin = users :jack
@non_admin = users :phil
end
test 'should get index only if admin' do
test_authorization(quotes_path, @admin, @non_admin) # green
end
#... rest of test ...
end

运行测试时的错误:

ERROR["test_authorization", #<Minitest::Reporters::Suite:0x00007f8f009fa7e0 @name="QuotesControllerTest">, 0.3059480000000008]
test_authorization#QuotesControllerTest (0.31s)
Minitest::UnexpectedError:         ArgumentError: wrong number of arguments (given 0, expected 3)
test/test_helper.rb:20:in `test_authorization'

为什么我得到这个错误,但仍然有我所有的测试工作正常?我没有正确创建测试助手吗?

不要以test_开头。

这是Minitest中的约定:所有以test_开头的内容都是测试。因此,它将被这样调用,不带参数。

最新更新