0

I am new to Capybara (using 2.1.0) and I cannot get it to fill in some fields in my brand-new (Rails 4/Ruby 2) application. Since it's just started, the app is simple.

For my User#new, I fill in the one field, which has a validation, and it saves when it's valid and fails when it's not. Great. I have another model, and try to do CoiReport#new and it does not work.

It keeps failing with "Owner cannot be empty" (it's got a validate_presence_of validator) even though if I save_and_open_page before click_button "Create New" the field is filled in. The field is an field, so I thought that might be it, but even when I changed it to type="text" it still fails. It also works when I do it by hand.

So both of these are very simple forms, but one works and one doesn't and I can't think what the difference is!

Here's the form:

<%= form_for(@coi_record) do |f| %>
  <% if @coi_record.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@coi_record.errors.count, "error") %> prohibited this coi_record from being saved:</h2>

      <ul>
      <% @coi_record.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :owner_id %><br>
    <%= f.text_field :owner_id %>
  </div>
  <div class="field">
    <%= f.label :signer_id %><br>
    <%= f.text_field :signer_id %>
  </div>
  <div class="field">
    <%= f.label :signed_at %><br>
    <%= f.datetime_select :signed_at %>
  </div>
  <div class="field">
    <%= f.label :is_current %><br>
    <%= f.check_box :is_current %>
  </div>
  <div class="field">
    <%= f.label :has_no_conflicts %><br>
    <%= f.check_box :has_no_conflicts %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And here's the test:

require 'spec_helper'

describe 'test2' do
  it "should work!" do

    visit '/coi_records/new'
    page.should have_content('New coi_record')
    within '#new_coi_record' do
      fill_in 'Owner', :with => '1'
      check 'Is current'
      check 'Has no conflicts'
      click_button 'Create Coi record'
    end
    # save_and_open_page  
    page.should have_content('Coi record was successfully created.')
  end
end

Help!

4

1 回答 1

0

好的,这对我来说是一个简单的飞行员错误。depa 完全正确,这不是Capybara 问题。问题出在我的验证器上。CoiRecord 因此与 User 相关联:

belongs_to :owner, class_name: 'User'

我希望这是一个必需的关系,所以我在 CoiRecord 中进行了验证,如

validates_presence_of :owner

它应该在哪里

validates_presence_of :owner_id

改变它可以让测试通过。菜鸟错误。多么尴尬!当然,我看了100遍,还是看不出来。depa 指出了方向,因为我可以看到正在设置值,所以我不得不想知道,为什么验证会失败......?

虽然如果 Rails 注意到并报告了一个不存在的字段的要求会很好......?

于 2013-09-09T17:23:32.397 回答