Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用Authlogic以编程方式登录用户?_Ruby On Rails_Authlogic - Fatal编程技术网

Ruby on rails 如何使用Authlogic以编程方式登录用户?

Ruby on rails 如何使用Authlogic以编程方式登录用户?,ruby-on-rails,authlogic,Ruby On Rails,Authlogic,我正在测试我的仪表板控制器,它需要用户登录才能访问。下面是我的代码 需要“测试助手” class DashboardControllerTest < ActionController::TestCase test "should redirect if not logged in" do get :index assert_response 302 end test "should get index if logged in" do user = User.

我正在测试我的仪表板控制器,它需要用户登录才能访问。下面是我的代码

需要“测试助手”

class DashboardControllerTest < ActionController::TestCase

 test "should redirect if not logged in" do
   get :index
   assert_response 302
 end

 test "should get index if logged in" do

   user = User.create(:username => "trivektor", :password => "abcd", :email =>   "trivektor@gmail.com", :first_name => "Tri", :last_name => "Vuong", :active => true)

   user_session = UserSession.create(:email => "trivektor@gmail.com", :password => "abcd")

   # get :index
   # assert_response 200
 end
end
类DashboardControllerTest“trivektor”,:password=>“abcd”,:email=>)trivektor@gmail.com,:first_name=>“Tri”,:last_name=>“Vuong”,:active=>true) user_session=UserSession.create(:email=>)trivektor@gmail.com“,:密码=>“abcd”) #获取:索引 #断言响应200 终止 终止 然而,我总是得到这个错误

Authlogic::Session::Activation::NotActivatedError:在创建对象之前,必须使用控制器对象激活Authlogic::Session::Base.controller


请帮忙。谢谢。

在创建新的用户会话之前,您需要激活authlogic,只需创建一个新的测试文件并添加以下内容:

class ActionController::TestCase
  setup :activate_authlogic
end