简体   繁体   中英

Authlogic session in Capybara doesn't exist immediately

My spec:

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

 context 'When user is logged in' do
    scenario 'Log in' do
      visit login_path

      within '.new_user_session' do
        fill_in 'username', with: user.email
        fill_in 'password', with: user.password
        click_on 'Log in'
      end

      visit new_search_path

      expect(page).to have_text "Welcome #{user.name}!"
    end
  end

The issue is that when visiting new_search_path , even though the login in successful, the page behaves as if there is no user logged in. If I add a sleep(1) call right before visit new_search_path , everything works fine.

Anyone know why this is happening?

I'm using authlogic, capybara, and selenium.

The action triggered.by click_on can occur asynchronously. Therefore, if you do a visit immediately after it can cause the login request to abort and the session cookies never get set. To solve that you need to check for page text/content that indicates the login has succeeded. Something like

expect(page).to have_text 'You are now logged in'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM