我正在用户注册表单上编写功能测试,并使用带有rspec的Capybara进行测试。 Capybara成功找到按钮,但测试结果返回并显示它在页面上找到的文本来自表单所在的页面,而不是之后加载的页面。我似乎无法找出为什么表单没有通过测试提交,我已经尝试了几种方法点击按钮,每次找到按钮但它不会触发表格发送。< / p>
Capybara测试失败错误
Failures:
1) submitting a new user creates a new user
Failure/Error: expect(page).to have_content("Traid with harold_houdini!")
expected to find text "Traid with harold_houdini!" in "Sign In Sign in with Google Sign In Sign in with Google First name: Last name: Birthday: Gender: Female Male Username: You can change your username whenever you'd like. Email: Password: Password confirmation: Address: Secondary address: City: State: Zip code: Country: What do you have to offer? What are you looking for?"
# /usr/local/rvm/gems/ruby-2.3.4/gems/given_core-3.8.0/lib/given/rspec/monkey.rb:21:in `handle_matcher'
# ./spec/features/users/new_spec.rb:16:in `block (2 levels) in <top (required)>'
Finished in 1.03 seconds (files took 3.33 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/features/users/new_spec.rb:5 # submitting a new user creates a new user
水豚测试
require 'capybara/rspec'
require 'rails_helper'
describe "submitting a new user" do
it "creates a new user" do
visit '/users/new'
within ("#new_user") do
fill_in "user[first_name]", with: "Harold"
fill_in "user[last_name]", with: "Houdini"
fill_in "user[username]", with: "harold_houdini"
fill_in "user[email]", with: "harold_houdini@gmail.com"
fill_in "user[password]", with: "password"
fill_in "user[password_confirmation]", with: "password_confirmation"
end
page.find("#new-user-form").click
expect(page).to have_content("Traid with harold_houdini!")
end
end
Rails Helper
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
# config.include Capybara::DSL
# Capybara.default_max_wait_time = 5
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
用户表单部分(已呈现)
<form class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="8CLe05BTkyXtB0f7HKRjqfcRhFNSdbrxg7DQeBLvxCNzLeCpPWyeK82V4VnIUjJfSvgmjYGiTCYLGtriDZV9kA==">
...........
<div>
<input type="submit" name="commit" value="submit" id="new-user-form">
</div>
</form>
答案 0 :(得分:0)
@hashrocket能够捕获错误,我根本就没有正确的密码/密码组合组合。更新后,我将我的按钮点击器更改为click_button(&#34; new-user-form&#34;),该按钮按ID定位并成功提交。