I work on a rails app, where user can signup.
Once the user is signed up, I would like to check that the persisted user record matches some expectations, for example: the first name has been saved.
it 'registers the user with the correct first_name' do
visit new_user_registration_path
within '#new_user' do
fill_in 'user[first_name]', with: 'Foo'
fill_in 'user[last_name]', with: 'Bar'
fill_in 'user[email]', with: 'foo@bar'
fill_in 'user[password]', with: 'password'
click_button
end
user = User.find_by(email: 'foo@bar')
expect(user.first_name).to eq('Foo')
end
end
In the example above, user is nil because the signup request didn't have time to complete yet. I have considered different options :
- only use
expect(page)when dealing with system tests - play with
page.document.synchronizeand wait for user to be available - use 'workaround' using each example specificity (e.g: using
User.last)
My understanding is that I should stick with option 1, but sometime user action have an effect that cannot is not rendered on the page.
What is according to you the best solution ?
the way I do it seems to be reliable, if your form is creating a User, you can do this:
if your form is editing a user you can do this: