无法找到未禁用的字段
Unable to find fields that is not disabled
所以我目前正在使用水豚和 rspec 进行一些测试,我遇到了这个错误消息
Users
Validation failed: First name can't be blank, Last name can't be blank, System generated password can't be blank
Capybara starting Puma...
* Version 3.11.4 , codename: Love Song
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:42509
Unable to find visible field "first_name" that is not disabled
Creates a new User
下面是我的 _form.html.haml 代码:
panel-body
= simple_form_for @user do |f|
.form-inputs
.col-md-6
= f.input :first_name, label: I18n.t('user.first_name')
= f.input :middle_name, label: I18n.t('user.middle_name')
= f.input :last_name, label: I18n.t('user.last_name')
= f.input :username, label: I18n.t('user.username')
.col-md-6
- if current_user.role? "Administrator"
= f.input :user_role, label: I18n.t('user.role'), collection: User::ROLES, include_blank: false
- else
= f.input :user_role, label: I18n.t('user.role'), collection: User::ROLES, include_blank: false, disabled: true
= f.hidden_field :user_role
.col-md-12
.form-actions
- if @user.new_record?
= f.button :submit, I18n.t('user.create_user'), class: "btn btn-primary"
= link_to I18n.t('cancel'), users_path, class: "btn btn-default"
- else
= f.button :submit, I18n.t('user.update_user'), class: "btn btn-primary"
= link_to I18n.t('cancel'), user_path(@user), class: "btn btn-default"
这是我的功能测试 (spec/features/users/create_user.rb):
require 'rails_helper'
RSpec.feature "Users", :type =>
:feature do
before do
begin
login_as(FactoryBot.create(:user, :admin)) # assumes you have a factory named `admin` that will create a user with the permissions to create other users
rescue StandardError => e
puts "#{e.message}"
end
end
it "Creates a new User" do
begin
visit "users/"
new_user = FactoryBot.build(:user)
fill_in "first_name", with: I18n.t('new_user.first_name') #new_user.first_name
fill_in "middle_name", with: I18n.t('new_user.middle_name')#new_user.middle_name
fill_in "last_name", with: I18n.t('new_user.last_name')#new_user.last_name
fill_in "username", with: I18n.t('new_user.username')#new_user.username
click_button "New User"
expect(page).to have_text "User was successfully created."
rescue StandardError => e
puts "#{e.message}"
end
end
end
下面是我的 factoryBot
#spec/factories/users.rb
FactoryBot.define do
factory :user do
first_name { 'System' }
last_name { 'Administrator' }
username {'admin'}
password {'password'}
system_generated_password {'password'}
trait :admin do
user_role {"Administrator"}
end
end
end
在 Capybara 启动服务器之前您收到验证消息 "Users Validation failed: First name can't be blank..." 的事实告诉我您的工厂没有生成有效的用户记录。您应该使用 FactoryBot linting 功能 - https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#linting-factories - to make sure all your factories produce valid records before the tests are run. You will also want to look into using sequences - https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#sequences - 用于工厂中具有唯一性要求的部分(例如 username
),以便它可以用于同时创建两个或多个记录。
注意:不需要 begin ... rescue .. end
块来包装所有内容 - 无论如何都会打印出任何引发的错误,通过拯救它们,您实际上是在防止有效的测试失败。
所以我目前正在使用水豚和 rspec 进行一些测试,我遇到了这个错误消息
Users Validation failed: First name can't be blank, Last name can't be blank, System generated password can't be blank Capybara starting Puma... * Version 3.11.4 , codename: Love Song * Min threads: 0, max threads: 4 * Listening on tcp://127.0.0.1:42509 Unable to find visible field "first_name" that is not disabled Creates a new User
下面是我的 _form.html.haml 代码:
panel-body
= simple_form_for @user do |f|
.form-inputs
.col-md-6
= f.input :first_name, label: I18n.t('user.first_name')
= f.input :middle_name, label: I18n.t('user.middle_name')
= f.input :last_name, label: I18n.t('user.last_name')
= f.input :username, label: I18n.t('user.username')
.col-md-6
- if current_user.role? "Administrator"
= f.input :user_role, label: I18n.t('user.role'), collection: User::ROLES, include_blank: false
- else
= f.input :user_role, label: I18n.t('user.role'), collection: User::ROLES, include_blank: false, disabled: true
= f.hidden_field :user_role
.col-md-12
.form-actions
- if @user.new_record?
= f.button :submit, I18n.t('user.create_user'), class: "btn btn-primary"
= link_to I18n.t('cancel'), users_path, class: "btn btn-default"
- else
= f.button :submit, I18n.t('user.update_user'), class: "btn btn-primary"
= link_to I18n.t('cancel'), user_path(@user), class: "btn btn-default"
这是我的功能测试 (spec/features/users/create_user.rb):
require 'rails_helper'
RSpec.feature "Users", :type =>
:feature do
before do
begin
login_as(FactoryBot.create(:user, :admin)) # assumes you have a factory named `admin` that will create a user with the permissions to create other users
rescue StandardError => e
puts "#{e.message}"
end
end
it "Creates a new User" do
begin
visit "users/"
new_user = FactoryBot.build(:user)
fill_in "first_name", with: I18n.t('new_user.first_name') #new_user.first_name
fill_in "middle_name", with: I18n.t('new_user.middle_name')#new_user.middle_name
fill_in "last_name", with: I18n.t('new_user.last_name')#new_user.last_name
fill_in "username", with: I18n.t('new_user.username')#new_user.username
click_button "New User"
expect(page).to have_text "User was successfully created."
rescue StandardError => e
puts "#{e.message}"
end
end
end
下面是我的 factoryBot
#spec/factories/users.rb
FactoryBot.define do
factory :user do
first_name { 'System' }
last_name { 'Administrator' }
username {'admin'}
password {'password'}
system_generated_password {'password'}
trait :admin do
user_role {"Administrator"}
end
end
end
在 Capybara 启动服务器之前您收到验证消息 "Users Validation failed: First name can't be blank..." 的事实告诉我您的工厂没有生成有效的用户记录。您应该使用 FactoryBot linting 功能 - https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#linting-factories - to make sure all your factories produce valid records before the tests are run. You will also want to look into using sequences - https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#sequences - 用于工厂中具有唯一性要求的部分(例如 username
),以便它可以用于同时创建两个或多个记录。
注意:不需要 begin ... rescue .. end
块来包装所有内容 - 无论如何都会打印出任何引发的错误,通过拯救它们,您实际上是在防止有效的测试失败。