如何停止 simple_form 将单选按钮设置为只读,给 Capybara 错误?
How to stop simple_form setting radio buttons as readonly, gives Capybara error?
我正在 'acceptance' 测试创建一个具有各种属性的 Quote
对象,表单是用 simple_form_for
制作的,f.input 指定 :radio_buttons
。当 运行 我的测试时:
creating quote request
Attempt to set readonly element with value: true
* This will raise an exception in a future version of Capybara
警告,提示表单输入有一个只读的html元素,我没有明确指定为true。 Capybara 似乎没有为其他输入元素类型设置值的问题,因此建议它们未设置为只读。
为什么 simple_form 将 radio_button 元素设置为只读,如果确实是这样的话?我如何覆盖它以便 Capybara 可以使用我在工厂中放置的值设置这些属性?
代码如下:
型号; quote.rb:
class Quote < ApplicationRecord
enum industry: [ :financial_services, :architect, :business_consultancy ]
enum payment_frequency: [ :annually, :monthly ]
end
控制器; quotes_controller.rb:
Class QuotesController < ApplicationController
def new
@quote = Quote.new
end
def create
@quote = Quote.new(quote_params)
if @quote.save
redirect_to root_url, notice: 'Quote request created'
else
render :new
end
end
def show
@quote = Quote.find(params[:id])
end
private
def quote_params
params.require(:quote).permit(:lives_overseas)
end
end
quote/new.html.erb
<div class='container'>
<div class='row'>
<div class='col-md-6'>
<%= simple_form_for @quote do |f| %>
<%= f.input :lives_overseas, as: :radio_buttons %>
<%= f.submit "Get quote", class: 'btn btn-primary' %>
<% end %>
</div>
</div>
</div>
为了清理测试,我将表单完成重构为 spec/support/new_quote_form.rb:
中的方法
class NewQuoteForm
include Capybara::DSL
def visit_page
visit('/')
self
end
def fill_in_with(params = {})
check('GLA') # How to fetch params option if given here?
within(".quote_prev_cover") { choose('No') } # How to fetch params option if given here?
fill_in('Company name', with: params.fetch(:co_name, 'acme co'))
fill_in('Company number', with: params.fetch(:co_number, '1234567'))
fill_in('Postcode', with: params.fetch(:postcode, 'al1 1aa'))
select('Financial services', from: 'Industry') # How to fetch params option if given here?
within(".quote_lives_overseas") { choose('No') } # How to fetch params option if given here?
within("#quote_scheme_start_date_1i") { select('2018') } # How to fetch params option if given here?
within("#quote_scheme_start_date_2i") { select('January') } # How to fetch params option if given here?
within("#quote_scheme_start_date_3i") { select('1') }
select('Monthly', from: 'Payment frequency') # How to fetch params option if given here?
fill_in('Commission level', with: params.fetch(:commission_level, '10'))
self
end
def submit
click_on('Get quote')
self
end
end
感谢@thomas-walpole,我现在意识到的实际测试是生成消息的测试; create_quote_spec.rb
require 'rails_helper'
require_relative '../support/new_quote_form'
feature 'creating quote request' do
let(:new_quote_form) { NewQuoteForm.new }
scenario 'completing quote data' do
new_quote_form.visit_page.fill_in_with().submit
expect(page).to have_content('Quote request created')
end
scenario 'cannot reqest quote with invalid data' do
new_quote_form.visit_page.submit
expect(page).to have_content("Must be selected")
end
end
Chrome 检查员显示此 html:
<input class="radio_buttons optional" readonly="readonly" value="false" name="quote[lives_overseas]" id="quote_lives_overseas_false" type="radio">
我试图注释掉 config/initializers/simple_form.rb; b.optional :readonly
但测试警告没有变化。
问题是此单选按钮的 false 选项中的 simple_form 错误设置 readonly="readonly"
。有两种解决方案,由 this post.
提供
1) 在 config/initializers/simple_form_bootstrap.rb
中注释掉 :vertical_radio_and_checkboxes
包装器中的 b.optional :readonly
。
2) 如下设置您的简单表单参数:
<%= f.input :prev_cover, as: :radio_buttons, collection: [['Yes', true],
['No', false]], readonly: nil %>
虽然选项 2 仍然有水豚在您 运行 测试时给您警告; Attempt to set readonly element with value: true * This will raise an exception in a future version of Capybara
我正在 'acceptance' 测试创建一个具有各种属性的 Quote
对象,表单是用 simple_form_for
制作的,f.input 指定 :radio_buttons
。当 运行 我的测试时:
creating quote request
Attempt to set readonly element with value: true
* This will raise an exception in a future version of Capybara
警告,提示表单输入有一个只读的html元素,我没有明确指定为true。 Capybara 似乎没有为其他输入元素类型设置值的问题,因此建议它们未设置为只读。
为什么 simple_form 将 radio_button 元素设置为只读,如果确实是这样的话?我如何覆盖它以便 Capybara 可以使用我在工厂中放置的值设置这些属性?
代码如下:
型号; quote.rb:
class Quote < ApplicationRecord
enum industry: [ :financial_services, :architect, :business_consultancy ]
enum payment_frequency: [ :annually, :monthly ]
end
控制器; quotes_controller.rb:
Class QuotesController < ApplicationController
def new
@quote = Quote.new
end
def create
@quote = Quote.new(quote_params)
if @quote.save
redirect_to root_url, notice: 'Quote request created'
else
render :new
end
end
def show
@quote = Quote.find(params[:id])
end
private
def quote_params
params.require(:quote).permit(:lives_overseas)
end
end
quote/new.html.erb
<div class='container'>
<div class='row'>
<div class='col-md-6'>
<%= simple_form_for @quote do |f| %>
<%= f.input :lives_overseas, as: :radio_buttons %>
<%= f.submit "Get quote", class: 'btn btn-primary' %>
<% end %>
</div>
</div>
</div>
为了清理测试,我将表单完成重构为 spec/support/new_quote_form.rb:
中的方法class NewQuoteForm
include Capybara::DSL
def visit_page
visit('/')
self
end
def fill_in_with(params = {})
check('GLA') # How to fetch params option if given here?
within(".quote_prev_cover") { choose('No') } # How to fetch params option if given here?
fill_in('Company name', with: params.fetch(:co_name, 'acme co'))
fill_in('Company number', with: params.fetch(:co_number, '1234567'))
fill_in('Postcode', with: params.fetch(:postcode, 'al1 1aa'))
select('Financial services', from: 'Industry') # How to fetch params option if given here?
within(".quote_lives_overseas") { choose('No') } # How to fetch params option if given here?
within("#quote_scheme_start_date_1i") { select('2018') } # How to fetch params option if given here?
within("#quote_scheme_start_date_2i") { select('January') } # How to fetch params option if given here?
within("#quote_scheme_start_date_3i") { select('1') }
select('Monthly', from: 'Payment frequency') # How to fetch params option if given here?
fill_in('Commission level', with: params.fetch(:commission_level, '10'))
self
end
def submit
click_on('Get quote')
self
end
end
感谢@thomas-walpole,我现在意识到的实际测试是生成消息的测试; create_quote_spec.rb
require 'rails_helper'
require_relative '../support/new_quote_form'
feature 'creating quote request' do
let(:new_quote_form) { NewQuoteForm.new }
scenario 'completing quote data' do
new_quote_form.visit_page.fill_in_with().submit
expect(page).to have_content('Quote request created')
end
scenario 'cannot reqest quote with invalid data' do
new_quote_form.visit_page.submit
expect(page).to have_content("Must be selected")
end
end
Chrome 检查员显示此 html:
<input class="radio_buttons optional" readonly="readonly" value="false" name="quote[lives_overseas]" id="quote_lives_overseas_false" type="radio">
我试图注释掉 config/initializers/simple_form.rb; b.optional :readonly
但测试警告没有变化。
问题是此单选按钮的 false 选项中的 simple_form 错误设置 readonly="readonly"
。有两种解决方案,由 this post.
1) 在 config/initializers/simple_form_bootstrap.rb
中注释掉 :vertical_radio_and_checkboxes
包装器中的 b.optional :readonly
。
2) 如下设置您的简单表单参数:
<%= f.input :prev_cover, as: :radio_buttons, collection: [['Yes', true],
['No', false]], readonly: nil %>
虽然选项 2 仍然有水豚在您 运行 测试时给您警告; Attempt to set readonly element with value: true * This will raise an exception in a future version of Capybara