Ruby on rails Capybara/Rspec-是否有方法在单击提交之前测试输入框是否已填充?
我正在学习RSpec和Capybara,并尝试测试用户是否可以导航到登录页面(由designe提供支持)并成功登录。测试未看到成功登录后应显示的页面。使用浏览器时,如果输入不存在,则返回登录页面。我使用的是Rails 5 登录规范rbRuby on rails Capybara/Rspec-是否有方法在单击提交之前测试输入框是否已填充?,ruby-on-rails,rspec,capybara,Ruby On Rails,Rspec,Capybara,我正在学习RSpec和Capybara,并尝试测试用户是否可以导航到登录页面(由designe提供支持)并成功登录。测试未看到成功登录后应显示的页面。使用浏览器时,如果输入不存在,则返回登录页面。我使用的是Rails 5 登录规范rb require 'spec_helper' require 'rails_helper' RSpec.feature "Logging in a User" do scenario "Logging in user shows special conten
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
水豚错误信息:
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)>'
1)登录用户登录用户显示特殊内容
失败/错误:第页应包含内容(“用户:”)
应在“登录\n邮件\n密码\n请记住我\n注册时忘记密码”中找到文本“用户”:
#./spec/features/login_spec.rb:17:in'block(2层)in'
是,您可以检查字段中是否填写了have\u字段
匹配器
expect(page).to have_field('Email', with: 'admin@example.com')
将验证页面是否有标签为“电子邮件”且填写值为“电子邮件”的字段admin@example.com"
这不是您当前问题的原因,但您是否有理由将rspecshould
和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)
您的Expedition行能够验证它是否已被填满,而不是
expect(当前路径)…
。我也很感激这些额外的提示。@jeepers\u brian如果这回答了你的问题,请接受答案(勾选),这样问题就会被标记为已回答。顺便说一句,我猜您的测试失败了,因为您在测试中没有实际创建具有这些凭据的用户(除非您遗漏了代码)