How to integration test forms with multiple models

Hi.

I try to create an integration test for a form that updates two models
(order and floorplan), Creating a functional test for this situation
works fine, however I cannot build an equivalent integration test.

My master model (order) contains
has_many :floorplans
accepts_nested_attributes_for :floorplans

that makes it possible to assign floorplans directly to order

then I have a form (new.html.erb) containing
<% form_for(@order, :html => {:multipart => true}) do |f| %>
    <% f.fields_for :floorplans do |floorplan_fields| %>
      <%= floorplan_fields.file_field :file_upload %>
    <% end %>
<% end %>

I only want my form to contain one floorplan object to be created, so
in the controller I have
  def new
    @order = Order.new
    @order.floorplans.build
  end

In the functional test I can now verify what I see in the browser:

  test "should create order" do
    assert_difference('Order.count') do
      post :create, {
        :order => {
          :floorplans_attributes =>
          [{:file_upload => fixture_file_upload('files/empty', 'image/png')}
          ]
        }}
    end

This works fine, but if I try to test this in an integration test I
can't test this, I have tried both

    post "orders/create", {:order => {
        :floorplans_attributes =>
        [ {:file_upload => fixture_file_upload('files/empty', 'image/png')} ]
      }}

and

    post "orders/create", {:order => {
        :floorplans_attributes_0_file_upload => fixture_file_upload('files/empty', 'image/png')
      }}

but it doesn't work.

The first attempt just serialises the list [ {:file_upload =>
fixture_file_upload('files/empty', 'image/png')} ] into a string when
creating the HTTP POST request and produces

    Hash or Array expected, got String ("file_upload#<ActionController::TestUploadedFile:0x7f47ee571f60>")

The second one fails with

    unknown attribute: floorplans_attributes_0_file_upload

What to do?

Jarl

Jarl Friis <jarl@gavia.dk> writes:

Hi.

I try to create an integration test for a form that updates two models
(order and floorplan), Creating a functional test for this situation
works fine, however I cannot build an equivalent integration test.

I figured out that this was a bug in rails test framework.

I have made some tests that demonstrates the problem and a patch that
solves the problem.

Both is available in ticket#2576
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2576

Jarl