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 =>
''test123-2iJpA2DrrpBSpG9eVto5HA@public.gmane.org'' }
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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Mar 1, 3:31 pm, Heinz Strunk <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 => ''test...-2iJpA2DrrpBSpG9eVto5HA@public.gmane.org'' } > 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?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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
So there''s no method to create the hash the way it''d be accepted? Sounds like a helper method... :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 1, 5:01 pm, Heinz Strunk <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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> -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---