# Problems using nested objects in a test

**URL:** <https://www.ruby-forum.com/t/problems-using-nested-objects-in-a-test/161712>\
**Category:** Rails\
**Created:** [March 1, 2009, 4:31pm UTC](https://www.ruby-forum.com/t/problems-using-nested-objects-in-a-test/161712 "2009-03-01T16:31:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [March 1, 2009, 4:31pm UTC](https://www.ruby-forum.com/t/problems-using-nested-objects-in-a-test/161712/1 "2009-03-01T16:31:07Z")

</div>

Hey,

I’m having some problems testing the creation of objects with nested  
objects. First of all, the test itself:  
def test\_should\_create\_user  
assert\_difference(‘User.count’) do  
post :create, :user =\> {  
:character =\> characters(:user\_controller\_test),  
:username =\> ‘test123’,  
:password =\> ‘test123’, :password\_confirmation =\> ‘test123’,  
:email =\> ‘removed\_email\_address@domain.invalid’ }  
end  
assert\_redirected\_to  
end

My problem is, that :character has an object as value and converting it  
to an arraay with .to\_a doesn’t really help cause the create method in  
the Users controller looks like:  
@nationality = Nationality.find(params[:character][:nationality])  
params[:character][:nationality] = @nationality  
params[:character][:birthday] =  
Date.strptime(params[:character][:birthday], ‘%d.%m.%Y’).to\_s(:db)  
@user = User.new(params[:user])  
@character = Character.new(params[:character])  
@user.character = @character

When I convert the characters(:user\_controller\_test) to an array it has  
:nationality\_id as a value but not :nationality as I’d have it when I  
was using a normal form.

Does anyone know how I could solve that problem?

One more thing… I’ve got many nested objects in User that’s why I need  
to create @nationality and @character in order to create @user and  
actually save it. Is there a more convenient way of doing this than I do  
it above?

Thanks

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [March 1, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/problems-using-nested-objects-in-a-test/161712/2 "2009-03-01T17:56:23Z")

</div>

On Mar 1, 3:31 pm, Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> :email =\> ‘removed\_email\_address@domain.invalid’ }  
> Date.strptime(params[:character][:birthday], ‘%d.%m.%Y’).to\_s(:db)  
> @user = User.new(params[:user])  
> @character = Character.new(params[:character])  
> @user.character = @character
> 
> When I convert the characters(:user\_controller\_test) to an array it has  
> :nationality\_id as a value but not :nationality as I’d have it when I  
> was using a normal form.
> 
> Does anyone know how I could solve that problem?

It’s up to you to create the sort of hash that would have been  
submitted normally. If the way your controller and test data work  
means that you have to spell that our at :character =\> {:nationality  
=\> characters(:user\_controller\_test).something.something …} then  
you’ve just got to get on with it

Fred

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [March 1, 2009, 6:01pm UTC](https://www.ruby-forum.com/t/problems-using-nested-objects-in-a-test/161712/3 "2009-03-01T18:01:08Z")

</div>

So there’s no method to create the hash the way it’d be accepted? Sounds  
like a helper method… 🙂

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [March 1, 2009, 6:04pm UTC](https://www.ruby-forum.com/t/problems-using-nested-objects-in-a-test/161712/4 "2009-03-01T18:04:39Z")

</div>

On Mar 1, 5:01 pm, Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> So there’s no method to create the hash the way it’d be accepted? Sounds  
> like a helper method… 🙂

Well given an activerecord object then foo.attributes is a hash of  
attributes, however this cannot anticipate how you’re handling things  
in the controller (eg nationality/nationality\_id and the thing you’re  
doing with dates)

Fred
