17
votes

I added some confirmation dialog boxes for my Rails 3.1 application and, prior to that, their corresponding tests. Following the model of Railscast #257, I added ':js => true' to the test, added database_cleaner and modified the spec_helper.rb file accordingly.

When I run the test, Firefox launches, Capybara-Selenium fills in the fields the the appropriate username and a password, but log-in fails (i.e., "invalid username/password".) Other tests that do not have ':js => true' and also login, do still pass.

I would like to add more javascript to my application in the future and I am avoiding solutions that would hack Capybara to get this to work (e.g., click 'OK' on all dialogs.)

Any ideas what I might be missing? Fail that, any suggestions on how to debug this problem?

Thank you.

2

2 Answers

15
votes

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...
  end
end

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.

9
votes

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
  end

end

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