3

This is the error I get when I run a cucumber test with @javascript with authlogic:

You must activate the Authlogic::Session::Base.controller with a controller object before creating objects

This is my authlogic support code in feature/support/authlogic.rb:

require "authlogic"
require "authlogic/test_case"
World(Authlogic::TestCase)

ApplicationController.skip_before_filter :activate_authlogic

Before do
  activate_authlogic
end

This is how I created a session:

def create_session 
  Session.create(:name => "test", :password => "test-33")
end

Without @javascript, it will not give me the error about authlogic not being activated, but with @javascript it does. How do I fix this problem?

Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
kiba
  • 81
  • 5

1 Answers1

7

Selenium and capybara-webkit use separate threads when launching processes. When you run activate_authlogic it does the following

Authlogic::Session::Base.controller = (@request && Authlogic::TestCase::RailsRequestAdapter.new(@request)) || controller

This winds up setting a thread local variable for :authlogic_controller. The problem is that this gets lost when you start using new threads in scenarios tagged with @javascript.

For me, the fix was to monkeypatch authlogic code like so

module Authlogic
  module Session
    module Activation
      module ClassMethods
        def controller
          if !Thread.current[:authlogic_controller]
            Thread.current[:authlogic_controller] = Authlogic::TestCase::MockController.new
          end
          Thread.current[:authlogic_controller]
        end
      end
    end
  end
end

This replicates what is done in acivate_authlogic. Make sure you only patch your test environment.

ActiveApathy
  • 1,171
  • 10
  • 19
  • I encountered this same issue when trying to use Cucumber and Capybara with Poltergeist as the javascript_driver (and PhantomJS as a headless browser). This fix worked for me. – shalott Mar 11 '14 at 00:24