2017-03-05 207 views
0

我无法使Clearance身份验证与Rails控制器单元测试一起使用。我遵循https://github.com/thoughtbot/clearance“控制器测试助手”的说明。你如何对需要认证的控制器进行单元测试?Rails间隙,单元测试控制器

我得到以下错误:

GoalsControllerTest#test_should_get_index: 
NoMethodError: undefined method `sign_in_as' for #<GoalsControllerTest:0x007f8c41c6b9c8> 
    test/controllers/goals_controller_test.rb:7:in `block in <class:GoalsControllerTest>' 

测试/ test_helper.rb中

require 'clearance/test_unit' 

测试/控制器/ goals_controller_test.rb

require 'test_helper' 

class GoalsControllerTest < ActionDispatch::IntegrationTest 
    setup do 
    user = User.new(fname: "Test", lname: "User", email: "[email protected]", password: "password") 
    sign_in_as(user) 
    @goal = goals(:one) 
    end 
+0

是在ApplicationController或辅助模块中定义的'sign_in_as'方法吗? – Linus

回答

0

您在使用Rails 5? Rails 5统一集成和控制器测试背后ActionDispatch::IntegrationTest。当您需要clearance/test_unit时,Clearance只会将其帮手添加到ActionController::TestCase

我认为你可以这样做:

class ActionDispatch::IntegrationTest 
    include Clearance::Testing::ControllerHelpers 
end 
test_helper.rb文件

,以获得访问这些测试的助手。但是,我不确定帮手本身会在这种情况下工作。

如果你可以试试,那会很有帮助。这也应该在Clearance中解决。由于我自己不使用TestUnit/MiniTest,所以有时候会错过这样的事情。

+0

用户= User.new(FNAME: “测试”,L-NAME: “用户”,电子邮件地址: “[email protected]”,密码: “密码”) sign_in_as(用户)
错误: NoMethodError:未定义的方法'env'for nil:NilClass test/controllers/goals_controller_test.rb:9:在' 我是Rails的新手。如果有任何有关单元测试许可的实例,将会有所帮助。 –