Login Failure when Testing with Capybara, Rspec and Selenium in Rails 3.1 Login Failure when Testing with Capybara, Rspec and Selenium in Rails 3.1 selenium selenium

Login Failure when Testing with Capybara, Rspec and Selenium in Rails 3.1


You should set use_transactional_fixtures = false for your seleniumtests.This can be done in the spec_helper.rb with

config.use_transactional_fixtures = false 

for all your tests.

Or do this for a single testcase:

describe 'testcase' , :type => :request do  self.use_transactional_fixtures = false  it 'your test', :js => :true do    testing...  endend

This happens because selenium-tests access the database in a different way. With transactional fixtures enabled, selenium will work on an empty database -> your user does not exist and you cannot login.

For normal tests you should use transactional fixtures because your tests run much faster.


As per Avdi Grimm's post (features a detailed explanation):

Gemfile:

group :test do  gem 'database_cleaner'end

spec_helper.rb:

config.use_transactional_fixtures = false 

spec/support/database_cleaner.rb:

RSpec.configure do |config|  config.before(:suite) do    DatabaseCleaner.clean_with(:truncation)  end  config.before(:each) do    DatabaseCleaner.strategy = :transaction  end  config.before(:each, :js => true) do    DatabaseCleaner.strategy = :truncation  end  config.before(:each) do    DatabaseCleaner.start  end  config.after(:each) do    DatabaseCleaner.clean  endend

I had transactional fixtures disabled but adding the :js => true block to database_cleaner.rb did it for me.