2017-03-05 206 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的新手。如果有任何有關單元測試許可的實例,將會有所幫助。 –