0

我正在测试上传带有黄瓜和嵌套属性的文档。目前,当我在 rails 服务器上运行时,它上传正常,但是当我尝试使用 capybaras should 方法测试结果时,它失败了。这是我的相关代码

错误信息

Then I should see the job posted and both files attached         # features/step_definitions/job_steps.rb:40
  expected there to be content "test_doc"

特征

Background:
Given I am logged in
And I am on the post a new job page

Scenario: User can post a job with a document and picture attached
  When I post a job with a picture and document attached
  Then I should see the job posted and both files attached

步骤定义

When /^I post a job with a picture and document attached$/ do
  fill_in 'Title', with: "Title"
  fill_in 'Description', with: "Description"
  fill_in 'Value', with: '199.99'
  fill_in 'job_job_documents_attributes_0_name', :with => "test_doc" 
  attach_file('job_job_documents_attributes_0_document', File.join(Rails.root, 'features', 'upload_files', 'test.pdf'))
  fill_in 'job_job_documents_attributes_0_name', with: "test_pic" 
  attach_file('job_job_pictures_attributes_0_picture', File.join(Rails.root, 'features', 'upload_files', 'test.jpg'))
  click_button 'Create Job'

Then /^I should see the job posted and both files attached$/ do
  page.should have_content 'Job Created!'   
  page.should have_content 'test_doc'       <------ FAILING LINE
end

形式

<%= form_for @job, :html => {name: 'job_form'} do |f| %>
  <%= f.label :title %> <br />
  <%= f.text_field :title %>

  <%= f.label :description %> <br /><br />
  <%= f.text_area :description %>

  <%= f.label :value %><br />
  <%= f.text_field :value %>

  <%= f.fields_for :job_documents do |document| %>
    <%= document.label :document %>
    <%= document.file_field :document %>

    <%= document.label :name %>
    <%= document.text_field :name %>
  <% end %>

  <%= f.fields_for :job_pictures do |picture| %>
    <%= picture.label :picture %>
    <%= picture.file_field :picture %>
    <%= picture.label :name %>
    <%= picture.text_field :name %>
  <% end %>
<%= f.submit %>

<% end %>

页面来源

<p>
    <label for="job_job_documents_attributes_0_document">Document</label>
    <input id="job_job_documents_attributes_0_document" name="job[job_documents_attributes][0][document]" type="file" />
  </p>
  <p>
    <label for="job_job_documents_attributes_0_name">Name</label>
    <input id="job_job_documents_attributes_0_name" name="job[job_documents_attributes][0][name]" size="30" type="text" />
  </p>
</p>

所以我所有的非嵌套属性都没有抛出任何错误,但我似乎无法让水豚看到我的嵌套属性。有任何想法吗?

4

1 回答 1

0

在新方法的控制器中,您需要构建job_documents,如下所示

@job = Job.new
@job.job_documents.build

也不要忘记在您的模型中将 job_documents_attributes 添加到 attr_accessible

于 2013-02-15T17:40:01.623 回答