2010-04-02 51 views
3

尽管遵循文档,但仍在获取以下错误。Authlogic和功能测试 - Authlogic :: Session :: Activation :: NotActivatedError:您必须激活

在test_helper.rb中

ENV["RAILS_ENV"] = "test" 
require File.expand_path(File.dirname(__FILE__) + "/../config/environment") 
require "authlogic/test_case" 
require 'test_help' 
require 'shoulda' 
require File.dirname(__FILE__) + "/factories" 

在我的功能测试 需要 'test_helper'

class SentencesControllerTest < ActionController::TestCase 
    setup do 
    :activate_authlogic 
    end 

    context "logged in" do 
    setup do 
     @user = Factory(:user) 
     UserSession.create(@user.id) 
    end 

    context "on GET to :new" do 
     setup do 
     get :new 
     end 

     should "present form with text field" do 
     assert_select('form#new_sentence') do 
      assert_select('textarea#sentence_text') 
     end 
     end 
    end 
    end #context logged in. 
end 

在environments.rb

config.gem "authlogic" 

林不知道为什么它不是工作。任何人都可以帮忙吗?

Authlogic::Session::Activation::NotActivatedError: You must activate the Authlogic::Session::Base.controller with a controller object before creating objects 
authlogic (2.1.3) lib/authlogic/session/activation.rb:47:in `initialize' 
    authlogic (2.1.3) lib/authlogic/session/klass.rb:64:in `initialize' 
    authlogic (2.1.3) lib/authlogic/session/scopes.rb:79:in `initialize' 
    authlogic (2.1.3) lib/authlogic/session/existence.rb:29:in `new' 
    authlogic (2.1.3) lib/authlogic/session/existence.rb:29:in `create' 
    test/functional/sentences_controller_test.rb:11:in `__bind_1270172858_922804' 
    shoulda (2.10.3) lib/shoulda/context.rb:380:in `call' 
    shoulda (2.10.3) lib/shoulda/context.rb:380:in `run_current_setup_blocks' 
    shoulda (2.10.3) lib/shoulda/context.rb:379:in `each' 
    shoulda (2.10.3) lib/shoulda/context.rb:379:in `run_current_setup_blocks' 
    shoulda (2.10.3) lib/shoulda/context.rb:371:in `run_all_setup_blocks' 
    shoulda (2.10.3) lib/shoulda/context.rb:375:in `run_parent_setup_blocks' 
    shoulda (2.10.3) lib/shoulda/context.rb:359:in `test: logged in on GET to :new should present form with text field. ' 
    /opt/rubymine/rb/testing/patch/testunit/test/unit/ui/testrunnermediator.rb:36:in `run_suite' 
    /opt/rubymine/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:215:in `start_mediator' 
    /opt/rubymine/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:191:in `start' 

回答

9

应:

class SentencesControllerTest < ActionController::TestCase 

    setup do 
    :activate_authlogic 
    end 

    ... 

是:

class SentencesControllerTest < ActionController::TestCase 

    def setup    # setup should be its own method, prefixed with "def" 
    activate_authlogic # note the lack of a ":" 
    end 

    ... 

或者,如果你关注了Rails测试教程,它可能有一个单行设置的交易,如:

setup :activate_authlogic # note the USE of a ":" here - not sure why it's different between this and when you put it in its own method but that might be the answer for you 
+0

斑点!非常感谢你 – robodisco 2010-05-03 04:17:52

+0

你懂了 - 我的快乐 – jefflunt 2010-05-04 01:14:14

+0

OMG,谢谢!我一直在挣扎几个小时! – 2010-10-17 16:29:04