我正在学习RSpec和Capybara,并试图测试用户是否可以导航到登录页面(由Devise提供支持(并成功登录。测试没有看到成功登录后应该出现的页面。使用浏览器时,如果没有输入,则返回登录页面。我使用的是Rails5。
login_spec.rb
require 'spec_helper'
require 'rails_helper'
RSpec.feature "Logging in a User" do
scenario "Logging in user shows special content" do
visit "/"
click_link "Sign In"
page.should have_content("Password")
#fill in login information
page.fill_in 'Email', with: 'admin@example.com'
page.fill_in 'Password', with: 'some_password'
click_on 'Log in'
page.should have_no_content("Wait for the text which is available in the sign in page but not on next page")
page.should have_content('User:')
expect(page.current_path).to eq(root_path)
end
end
Capybara错误消息:
1) Logging in a User Logging in user shows special content
Failure/Error: page.should have_content('User:')
expected to find text "User:" in "Log innEmailnPasswordnRemember menSign up Forgot your password?"
# ./spec/features/login_spec.rb:17:in `block (2 levels) in <top (required)>'
是的,您可以检查字段是否已用have_field
匹配器填充
expect(page).to have_field('Email', with: 'admin@example.com')
将验证页面是否有一个标签为"电子邮件"且填写值为"的字段admin@example.com"。
这不是您当前问题的原因,但您混合使用RSpecsshould
和expect
语法有什么原因吗?你真的应该坚持一个,最好是"期待新的代码-所以
expect(page).to have_content("Password")
expect(page).not_to have_content("Wait for the text which is ...
而不是
page.should have_content("Password")
page.should have_no_content("Wait for the text which is ...
此外,您几乎不应该将普通的RSpec匹配器(eq
等(与任何与Capybara相关的东西一起使用,相反,您应该使用Capybara提供的匹配器
expect(page).to have_current_path(root_path)
而不是expect(current_path)...