[英]Test devise after_inactive_sign_up_path_for confirmable redirect
[英]How to test devise after sign in path with Capybara
我正在尝试测试通过Devise gem登录后会发生什么。 例如,在用户成功登录后,我可以让控制器转到student_dashboard_path。
如何使用Capybara和Rspec进行测试?
我目前在:
/spec/features/user_signs_in_sees_dashboard_spec.rb
require 'rails_helper'
feature 'User sign in' do
scenario 'successfully from sign in page and sees student dashboard' do
sign_in
visit student_dashboard_path
expect(current_path).to eq(student_dashboard_path)
end
end
而我在:
/spec/support/features/sign_in.rb
module Features
def sign_in
visit user_session_path
fill_in 'Email', with: User.first.email
fill_in 'Password', with: User.first.password
click_button 'Log in'
end
end
我收到此错误消息:
1) User signs in successfully from sign in and sees dashboard
Failure/Error: expect(current_path).to eq(student_dashboard_path)
expected: "/student/dashboard"
got: "/student/login"
我不确定为什么无法登录并查看学生仪表板。
我将下面的原始答案留给仍在Capybara <2.5.0以下的任何人,但是在2.5.0中,您现在可以
expect(page).to have_current_path(<expected path>)
它会在检查路径时使用Capybara的等待行为
----以下仅适用于<2.5.0的Capybara版本
Expect(current_path).to eq(...)并不等待路径更改,它只是与调用时的当前路径进行比较。 如果您在单击按钮后睡了,我敢打赌,它可以工作。 更好的解决方案是使用类似
expect(page).to have_text('You are now logged in')
在click_button之后。 这将导致水豚等待登录完成,页面加载(并且出现登录通知),并因此直到current_path也更改为止。
使用中间的save_and_open_page
来确定是否正确设置了字段。 如果您在同一台计算机上工作,则可以切换到硒以在真实的浏览器上进行测试。
另外请确保此代码不需要任何JS即可工作,因为默认的水豚匹配器将无法运行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.