如何将simple_form设置单选按钮停止为只读状态,会导致水豚错误?
我是'验收'测试创建一个Quote
对象与各种attrs,表格是由simple_form_for
与f.input指定:radio_buttons
。当运行我的测试中的:给出如何将simple_form设置单选按钮停止为只读状态,会导致水豚错误?
creating quote request
Attempt to set readonly element with value: true
* This will raise an exception in a future version of Capybara
警告,提示的形式输入具有只读html元素,这是不明确由我指定为真。水豚似乎没有为其他输入元素类型设置值的问题,因此表明它们没有设置为只读。
为什么simple_form将radio_button元素设置为readonly,如果这确实是这里发生的事情?我如何重写这一点,以便水豚能够用我在工厂中的价值来设定这些价值?
代码如下:
Model; 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
报价/ 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>
要清理检查我重构形式完成成规格/支持/ 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检查表明这个网站:
<input class="radio_buttons optional" readonly="readonly" value="false" name="quote[lives_overseas]" id="quote_lives_overseas_false" type="radio">
我已经尝试注释掉配置/初始化/ simple_form.rb; b.optional :readonly
但没有更改测试警告。
问题是这个单选按钮的false选项中的simple_form bug设置readonly="readonly"
。有两种解决方案,this提供。
1)在config/initializers/simple_form_bootstrap.rb
内注释掉b.optional :readonly
内的:vertical_radio_and_checkboxes
包装。
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
你得到这个是因为你的测试告诉水豚设置readonly元素的值,如果你不想看到错误,那么不要尝试和修改页面上的只读元素。这与FactoryGirl无关,并且与您的测试正在尝试做什么有关。 –
您的'Quote'模型是否指定'attr_readonly'或覆盖'readonly_attributes'?此外,这些实际上并未显示创建错误代码的小文件剪辑(have_content未在页面上设置只读字段)使人们很难帮助您 - 请参阅https://*.com/帮助/如何提问 –
模型没有明确指定'attr_readonly'或覆盖'readonly_attributes'。似乎是simple_form默认设置readonly attrs为真,对输入指定为:radio_buttons? – jbk