I've noticed some strange behavior ONLY in feature tests when a user is not logged in and they aren't being redirected to the sign in page and I'm trying to understand why. I'm using poltergeist as the javascript driver.
Here's a couple of examples:
# works as expected
it 'redirects to sign_in page when not logged in' do
get :index, {}
expect(response).to eq('/users/sign_in')
end
# does not redirect to '/users/sign_in'
it 'should redirect to the user sign in page' do
visit '/'
expect(page.current_path).to redirect_to('/users/sign_in')
end
Does anyone know why devise isn't redirecting to the sign_in page in feature tests?
You can't use redirect_to with Capybara -- Capybara doesn't provide data on whether the page is redirecting it just provides the url it's on as a string. You probably want
expect(page).to have_current_path('/users/sign_in')
Note: a naive way to attempt to check the same thing would be to do expect(page.current_path).to eq('/users/sign_in')
however that would check the current_path immediately, which will cause flaky tests when used with JS capable drivers since most actions in those drivers can occur asynchronously. Using the have_current_path
matcher adds in waiting/retrying behavior and is therefore the correct method to use.
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.