capybara无法找到带有集合的simple_form字段

时间:2016-04-22 16:32:06

标签: ruby-on-rails ruby rspec capybara

我有带字段的simple_form表单

filtered

它工作正常。但是当我用水豚测试它时,它给了我一个错误:     故障:

<%= simple_form_for(@menu) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">enter code here
    <%= f.input :order_date, :input_html => { :value => Time.now.strftime("%A") } %>
    <%= f.input :title %>
    <%= f.input :dish_category, input_html: {id: "dish_category"}, as: :radio_buttons,  collection: ["First course", "Main course", "Drink"] %>
    <%= f.input :description %>
    <%= f.input :price %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

测试本身:

  1) Lunches Admin create menu lunches admin can create new menu only for today
     Failure/Error: fill_in 'Dish category', :with => 'First course'

     Capybara::ElementNotFound:
       Unable to find field "Dish category"

HTML本身:(我必须写更多内容,因为该程序说它是许多代码而不是单词)

  scenario "lunches admin can create new menu only for today" do
    admin = FactoryGirl.create(:lunches_admin)
    login_as(admin)
    visit new_menu_path
    fill_in "Order date",    :with => Time.now.strftime("%A")
    fill_in "Title",         :with => "Borsh"
    fill_in "Description",   :with => "Mmm, yamy"    
    fill_in "Price",         :with => 99.99 
    fill_in 'Dish category', :wit`enter code here`h => 'First course'
    click_button 'Create Menu'
    expect(page).to have_content "Menu was successfully created."
  end

1 个答案:

答案 0 :(得分:0)

由于dish_category是单选按钮,因此您需要使用另一个水豚选择器才能与其进行交互。 变化

fill_in 'Dish category', :with => 'First course'

为:

choose('First course')

这应该可以解决问题。

您可以在此处详细了解表单的水豚选择器:https://github.com/jnicklas/capybara#interacting-with-forms