Forum: Ruby on Rails How to integration test forms with multiple models

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
407016c9d4109e726fc30681b21324cc?d=identicon&s=25 Jarl Friis (jarl)
on 2009-04-27 13:06
(Received via mailing list)
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
407016c9d4109e726fc30681b21324cc?d=identicon&s=25 Jarl Friis (jarl)
on 2009-04-29 08:54
(Received via mailing list)
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...

Jarl
This topic is locked and can not be replied to.