我正在学习RSpec和Capybara,并尝试测试用户是否可以导航到登录页面(由Devise提供支持)并成功登录。成功登录后,测试没有看到应显示的页面。使用浏览器时,如果不存在输入,它将返回登录页面。我正在使用Rails 5。
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: '[email protected]'
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
水豚错误消息:
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 in\nEmail\nPassword\nRemember me\nSign 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: '[email protected]')
将验证页面上带有标签为'Email'和填充值为'[email protected]'的字段。
这不是当前问题的原因,但是是否有原因混合使用RSpecs should
和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)...