# How to integration test forms with multiple models

**URL:** <https://www.ruby-forum.com/t/how-to-integration-test-forms-with-multiple-models/166567>\
**Category:** Rails\
**Created:** [April 27, 2009, 1:06pm UTC](https://www.ruby-forum.com/t/how-to-integration-test-forms-with-multiple-models/166567 "2009-04-27T13:06:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jarl](https://www.ruby-forum.com/u/jarl)\
**Post date:** [April 27, 2009, 1:06pm UTC](https://www.ruby-forum.com/t/how-to-integration-test-forms-with-multiple-models/166567/1 "2009-04-27T13:06:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jarl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jarl](https://www.ruby-forum.com/u/jarl)\
**Post date:** [April 29, 2009, 8:54am UTC](https://www.ruby-forum.com/t/how-to-integration-test-forms-with-multiple-models/166567/2 "2009-04-29T08:54:00Z")

</div>

Jarl F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2576)

Jarl
