My devise user is :confirmable
, and I want to test that the user is redirected to the devise login page after signing up. It works when I manually test it, but fails in rspec/capybara feature spec. I'm following these instructions to setup the redirect.
# registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(_resource)
new_user_session_path # redirect to login
end
end
# registration_spec.rb
RSpec.describe 'registration process', type: feature do
it 'works' do
visit new_user_registration_path
fill_in 'Username', with: 'user123'
fill_in 'Email', with: 'user123@email.com'
fill_in 'Password', with: 'password123'
fill_in 'Password confirmation', with: 'password123'
click_on 'Sign up'
expect(User.find_by(username: 'user123')).not_to be_nil # sanity check
expect(page).to have_current_path(new_user_session_path) # failure here
end
end
Failure returned:
Failure/Error: expect(page).to have_current_path(new_user_session_path)
expected "/users" to equal "/users/sign_in"
# ./spec/features/registration_spec.rb:19:in `block (2 levels) in <top (required)>'
It seems like page
is stuck in the post path, or is being redirected to /users
incorrectly? I know the form is accepting the input because of my sanity check that the user exists.
Most likely your user creation is failing, whether that's due to an existing user or the password not meeting minimum security requirements, etc I'm not sure. Outputting page.html
after the click should show you any validation errors, or your test.log
should show them too.
Additionally, click_on
can be asynchronous (when using a JS capable driver) so you should swap the last two lines of your test, because have_current_path will wait for the page change to occur which implies the user creation has completed. That won't fix your current issue, but using the Capybara matchers to make sure actions have completed before running direct database queries will reduce potential flakiness for you in the future (Note, however, that direct database queries are generally a bad code smell in features tests).
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.