Rspec - You must activate the Authlogic::Session::Base.controller - I've already tried the common suggestions

StackOverflow https://stackoverflow.com/questions/14927543

Вопрос

I know this question has been replied many times, but the existing answers are not resolving my issue.

I want to run my rspec tests for the controllers. I need to authenticate the user through Authlogic.

As many of you said (in other similar questions), I added to the spec_helper.rb file:

require "authlogic/test_case"
include Authlogic::TestCase

and activate_authlogic in the before each in my events_controller_spec.rb:

...
  before :each do
    activate_authlogic
    user = FactoryGirl.create(:user)
    UserSession.create(user)
  end
...

but running the rspec, I get always the infamous error message:

Failure/Error: UserSession.create(user)
     Authlogic::Session::Activation::NotActivatedError:
       You must activate the Authlogic::Session::Base.controller with a controller object before creating objects

Can you see what's wrong on it ?

Any suggestions are really welcome.

Thanks!

ps.

Versions used: - rails 3.2 - rspec (2.11.0) - rspec-core (2.11.1)

Это было полезно?

Решение 3

After many tries I've found more that one issues:

  • the user created by FactoryGirl was not a valid user
  • instead of "before :each" sometimes I tried "before :all", and that added noise to my bug fixing

Sorry for the "not so useful" question :)

Другие советы

try using setup :activate_authlogic or Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self) instead of activate_authlogic, maybe it will help

I don't understand if you have found a solution, but here is the code I use to my Rspec controllers. You can catch the error of non-valid user before session creation !

spec/spec_helper.rb

def log_in(user)
  user.should_not be_nil
  session = UserSession.create!(user, false)
  session.should be_valid
  session.save
end

spec/controllers/...

require 'spec_helper'
require 'authlogic/test_case'

describe xxxController do
  setup :activate_authlogic

  context "user logged" do
    before(:each) do
      @user = Factory(:user)
      log_in(@user)
    end
  end
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top