So I was using Rolify, Devise, Rspec and Capybara Webkit in Rails and I ran into a problem with the Warden login_as helper when trying to do a feature spec with :js => true (if you didn't understand any of that, you are probably on the wrong blog).
Anyways, while the I was able to log in an admin, Rolify was not able to determine the roles. This is in spite of the fact that I added a role in Factory Girl.
So the answer had to do with the following line in rails_helper.rb
config.use_transactional_fixtures = true
So I believe what happened in Capybara webkit is that it runs in a different thread and while Warden's login_as worked fine in a regular spec (as the admin object is passed into memory), the role information (which needs a database lookup) was not passed through and hence when I was checking that the admin was an administrator (current_admin.has_role? :administrator) it failed.
So here's the fix
1) Install the database_cleaner gem
2) set transactional fixtures to false
config.use_transactional_fixtures = false
3) Set up database cleaner as per http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/
4) in your rails_helper.rb
require 'support/database_cleaner'
Your Capybara Webkit Rolify specs should now work with the Warden helpers.
PS Sorry this isn't that well written. This is primarily a reminder for myself (I am still in mid project), but I thought I would post it in case anyone else ran into the same issue.
Anyways, while the I was able to log in an admin, Rolify was not able to determine the roles. This is in spite of the fact that I added a role in Factory Girl.
FactoryGirl.define do factory :admin do email { Faker::Internet.email } password "password" password_confirmation "password" trait :administrator do after(:create) {|admin| admin.add_role(:administrator)} end end endThis worked fine with js turned off, so why was it not working now?
So the answer had to do with the following line in rails_helper.rb
config.use_transactional_fixtures = true
So I believe what happened in Capybara webkit is that it runs in a different thread and while Warden's login_as worked fine in a regular spec (as the admin object is passed into memory), the role information (which needs a database lookup) was not passed through and hence when I was checking that the admin was an administrator (current_admin.has_role? :administrator) it failed.
So here's the fix
1) Install the database_cleaner gem
2) set transactional fixtures to false
config.use_transactional_fixtures = false
3) Set up database cleaner as per http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/
4) in your rails_helper.rb
require 'support/database_cleaner'
Your Capybara Webkit Rolify specs should now work with the Warden helpers.
PS Sorry this isn't that well written. This is primarily a reminder for myself (I am still in mid project), but I thought I would post it in case anyone else ran into the same issue.
Comments