Daniel Evans Daniel Evans - 1 year ago 92
Ruby Question

Rails 3 with Devise and rspec: Warden test helpers unreliable

I have an application using rails 3.2 and Devise. I have a request test suite using rspec and Capybara.

I have attempted to shift to using the Warden test helpers in my sign in helper rather than having Capybara fill in the sign in form and submit it. Due to the size and complexity of my test suite, this results in over a minute and a half of savings in my test runtimes.

In my configuration is:

RSpec.configure do |config|
config.include Warden::Test::Helpers, :type => :request
config.after :each do

And in a Context:

let!(:current_user) { FactoryGirl.create(:user) }

background do
login_as(current_user, :scope => :user)

However, when running the test suite using these configurations nearly every test run has a different randomly failing request spec due to the page presenting as though the user were not logged in. (Specifically, links controlled by a

My question: Has anyone else encountered such unreliability and how can it be mitigated?

Answer Source

The absolute easiest thing to do is just use the sign_in form in your before :each block. You need the test helpers in Controller specs, because it's isolated from devise. You don't have that problem in an integration test.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download