← Back to Upcase

Clearance Backdoor and clicking links


(Matthew Sumner) #1

It appears that the testing backdoor in clearance only works for the path you visit first. I have been writing integration tests starting from the root page to give a full test of completing various tasks. Here is a failing test I have:

require 'spec_helper'
feature 'Manage patients' do
  scenario 'create a new patient' do
    user = create :user
    visit root_path(as: user)
    click_link 'Patients'
    click_link 'New Patient'

    fill_form(:patient, first_name: 'john', last_name: 'doe'))
    click_on submit(:patient)

    expect(page).to have_content I18n.t('patient.create.success')
    expect(page).to have_content 'john doe'
  end
end  

However, this test is failing at click_link 'New Patient' saying it can’t find the link. This is because the app is redirecting to the sign_in path after clicking the ‘Patients’ link. Looking at the backdoor code it looks like it actually creates a new session and logs in the test so I feel like this should be working.

My patients_controller.rb has before_filter :authorize. No special user type required at the moment.

Should I log in the old fashioned way for these tests? It feels like the backdoor should work for clicking links otherwise it’s no good for integration tests.

Edit 1

So if I change my test to check for a current user e.g.

require 'spec_helper'
feature 'Manage patients' do
  scenario 'create a new patient' do
    user = create :user
    visit root_path(as: user)

    expect(signed_in_user).not_to be nil

    click_link 'Patients'
    ...
  end
end

The test works as expected…