Capybara不会填写表格字段(虽然它找到了)

我正在使用Ruby 2.2.1和Capybara 2.2.4开发Rails 4.2.1。 编写规范来注册用户,Capybara确实找到了字段(我没有得到ElementNotFound错误),但是当我尝试validation时,我发现所有字段都是空的并且在打开launchy之前告诉Capybara单击注册按钮显示所有字段为空。

规格:

require 'rails_helper' RSpec.feature "Sign Up Users", type: :feature do scenario "saves user to the database with valid data" do visit root_path click_link "Sign Up" find("#user_name").set("John Doe") fill_in "user_username", with: "johndoe" fill_in "user_email", with: "john@example.com" fill_in "user_password", with: "helloworld" fill_in "user_password_confirmation", with: "helloworld" save_and_open_page click_button "Create Account" expect(page).to have_text('Account successfully created! Welcome!') expect(page).to have_link('Log Out') expect(page).not_to have_link('Sign In') expect(page).not_to have_link('Sign Up') end scenario "does not save user with invalid data and re-renders sign up page" do visit root_path click_link "Sign In" click_link "Sign In" expect(page).to render(:signin) end end 

RSpec错误:

 Failures: 1) Sign Up Users saves user to the database with valid data Failure/Error: expect(page).to have_text('Account successfully created! Welcome!') expected to find text "Account successfully created! Welcome!" in "Menu Sign In Sign Up TUDO | PRESENTES | FESTA | NOIVO | NOIVA Sign Up Please review the problems below: * Namecan't be blank * Usernamecan't be blank * Email * Password * Password confirmation Log in" # ./spec/features/sign_up_users_spec.rb:18:in `block (2 levels) in ' Finished in 0.97155 seconds (files took 2.09 seconds to load) 10 examples, 2 failures, 2 pending 

编辑:根据要求,表格erb模板:

  

并生成的forms:

 
Sign Up
Log in

我已经成功解决了这个问题。 它是由强大的参数和设计引起的。 我已将Devise设置为使用名称和用户名参数,即使在控制器中明确清除,它们也未被授权。 这是我做的:

  1. 生成的设计控制器:

    $ rails g devise:controllers

  2. 将新的参数添加到controllers / user / registrations_controller.rb中的默认参数:

#You can put the params you want to permit in the empty array.

 def configure_account_update_params puts "configure_account_update_params" devise_parameter_sanitizer.for(:account_update) << :name devise_parameter_sanitizer.for(:account_update) << :username end 
  1. 将路由更改为首先通过此控制器而不是routes.rb中的默认设计:

    devise_for :users, :controllers => { :registrations => "user/registrations" }

  2. 取消注释registrations_controller.rb中的第一行:

    before_filter :configure_sign_up_params, only: [:create]

这不是一个水豚问题,而是一个具有强烈参数问题的设计。 现在它的工作完美了。