我正在开发一个 Rails 应用程序,用户可以在其中注册。
用户注册后,我想检查持久的用户记录是否符合某些期望,例如:名字已被保存。
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
在上面的示例中,用户为零,因为注册请求尚未完成。 我考虑过不同的选择:
expect(page)
page.document.synchronize
并等待用户可用User.last
)我的理解是我应该坚持使用选项 1,但有时用户操作会产生无法不在页面上呈现的效果。
您认为最好的解决方案是什么?
我的做法似乎很可靠:
expect{click_button}.to change{User.count}.by(1)
user = User.find_by(email: 'foo@bar')
expect(user.first_name).to eq('Foo')