Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    Warden.test_reset!
  end
end

And in a Context:

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

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

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 user_signed_in? conditional)

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

like image 995
Daniel Evans Avatar asked Jun 11 '12 20:06

Daniel Evans


1 Answers

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.

like image 122
DVG Avatar answered Oct 19 '22 23:10

DVG