我接受'测试使用各种attrs创建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
提供了警告,表明表单输入有一个readonly html元素,我没有明确指定为true。 Capybara似乎没有为其他输入元素类型设置值的问题,因此建议它们不被设置为只读。
为什么simple_form将radio_button元素设置为readonly,如果这确实在这里发生了什么?我如何覆盖这一点,以便Capybara可以设置这些attrs与我在工厂的价值?
代码如下:
模型; 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>
要清理测试,我将表单完成重构为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
但测试警告没有变化。
答案 0 :(得分:0)
问题是这个单选按钮的false选项中的simple_form错误设置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仍然让Capybara在你进行测试时给你一个警告; Attempt to set readonly element with value: true * This will raise an exception in a future version of Capybara