这个失败的单选按钮功能是否会测试Capybara语法问题?

时间:2017-06-08 11:07:26

标签: ruby-on-rails rspec model capybara

我有一个带有布尔attr prev_cover的单选按钮的表单,在浏览器中显示是或否,参数值为true或false,并在db中保存为true或false,如数据类型boolean。我想:

  • a)模拟测试存在选择的单选按钮
  • b)功能测试创建整个报价记录。

通过验证prev_cover,我告诉capybara within(".quote_prev_cover") { choose('No') } create_quote_spec测试失败,模型quote.rb测试通过。

通过验证prev_cover,我告诉capybara {_ 1}} create_quote_spec测试通过,模型quote.rb测试通过。

在实际的手动浏览器测试中,当选择“是”或“否”时,将创建数据库记录。

当我从quote.rb中删除prev_cover的状态验证时,create_quote_spec会随prev_cover选择是或否,但当然模型quote_spec.rb测试失败。

我无法看到问题出在哪里,是否与capybara within(".quote_prev_cover") { choose('Yes') }语法有关? (它发现css元素很好)

这是new.html.erb

的表单元素
within(".quote_prev_cover") { choose('No') }

这里是quote.rb验证;

<div class='container'>
    <div class='row'>
        <div class='col-md-6'>
            <%= simple_form_for @quote do |f| %> 
                <%= f.input :prev_cover, as: :radio_buttons, label: "Previous cover" %>
                <%= f.submit "Get quote", class: 'btn btn-primary' %>
            <% end %>
        </div>
    </div>
</div>

这是由Capybara创建的名为NewQuoteFrom.new的validates :prev_cover, inclusion: { in: [true, false] } 元素;

prev_cover

如果选择prev_cover&#39; No&#39;这里的测试失败了如果选择&#39;是&#39;,create_quote_spec.rb

,则传递
within(".quote_prev_cover") { choose('No') } 

这里是模型验证存在时通过的模型测试,quote_spec.rb;

feature 'creating quote request' do
let(:user) { FactoryGirl.create(:user) }
let(:new_quote_form) { NewQuoteForm.new }

before do
    login_as(user, :scope => :user) 
end

scenario 'completing quote data' do
    new_quote_form.visit_page.fill_in_with().submit
    expect(page).to have_content('Quote request created')
end

测试消息失败:

    context 'previous cover' do
        it 'must be true or false' do
            quote = Quote.new(prev_cover: nil)
            quote.valid?
            expect(quote.errors[:prev_cover]).not_to be_empty
        end 
    end

请注意错误消息中的1) creating quote request completing quote data Failure/Error: expect(page).to have_content('Quote request created') expected to find text "Quote request created" in "Toggle navigation QuoteEngine My Quotes My Account Sign out Complete the below to get a quote * GLA Previous coverYesNois not included in the list * Company name * Company number * Postcode * Industry Financial services Architect Business consultancy Lives overseasYesNo Scheme start date 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 January February March April May June July August September October November December 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Payment frequency Annually Monthly Commission level" # ./spec/features/create_quote_spec.rb:14:in `block (2 levels) in <top (required)>' ,使用Capybara指令中的任何true或false变体都不起作用。

甚至指定:

YesNois not included in the list
new.html.erb视图中的simpl_form_for中的

仍会导致验证失败,并提供<%= f.input :prev_cover, as: :radio_buttons, :collection => [['Yes', true], ['No', false]], label: "Previous cover" %>

即使Previous coverYesNois not included in the list仍然失败,也会使用相同的within(".quote_prev_cover") { choose('quote_prev_cover_false') }

如果被告知选择是Previous coverYesNois not included in the list,则通过,如果我还没有从within(".quote_prev_cover") { choose('Yes') }更改为presence:,我肯定已经这样做了。

来自firefox检查器的实际HTML:

inclusion:

2 个答案:

答案 0 :(得分:1)

此报告唯一有意义的方法是,如果您选择了“是”或“否”,并且创建了相应的布尔值,那么您的语句“在实际手动浏览器中测试数据库记录是否正确”。不是真的。

那是因为您正在使用validates_presence_of(通过验证在线状态:{...}快捷方式)验证程序,如果false是有效响应,则不能与布尔字段一起使用。来自validates_presence_of文档

  

如果要验证是否存在布尔字段(其中   你会想要使用真正的价值观是真是假   validates_inclusion_of:field_name,in:[true,false]。

     

这是由于Object#blank的方式?处理布尔值:   false.blank? #=&gt;真。

因此,如果您从状态验证更改为

validates :prev_cover, inclusion: { in: [true, false] }

你的测试可能会通过。

答案 1 :(得分:0)

导致此Capybara测试尝试创建引用记录失败的问题是它无法选择布尔单选按钮的No / false值,这是由于simple_form错误设置了readonly="readonly"。这篇文章here

提供了两种解决方案

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

感谢#ThomasWalpole一路帮助我和platformatec维护人员。