kiba
kiba

Reputation: 81

Authlogic with Capybara + Cucumber + Selenium Driver not working

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?

Upvotes: 3

Views: 769

Answers (1)

ActiveApathy
ActiveApathy

Reputation: 1211

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.

Upvotes: 7

Related Questions