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

时间:2011-03-30 16:52:01

标签: ruby-on-rails authlogic

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

要求'test_helper'

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

但是,我总是得到这个错误

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

请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

您需要在创建新的UserSession之前激活authlogic,只需创建一个新的测试文件并添加:

class ActionController::TestCase
  setup :activate_authlogic
end
相关问题