Rails Functional Test Failing Due to Association
Posted
by Koby
on Stack Overflow
See other posts from Stack Overflow
or by Koby
Published on 2010-05-24T04:05:28Z
Indexed on
2010/05/24
4:10 UTC
Read the original article
Hit count: 216
ruby-on-rails
|testing
I have an accounts model that holds some basic account info (account name, website, etc). I then have a user model that has the following in the app/models/user.rb
belongs_to :account
I also have the following in my routes.rb
map.resources :account, :has_many => [:users, :othermodel]
the problem I'm facing is that the following test is failing:
test "should create user" do
assert_difference('User.count') do
post :create, :user => { } #this is the line it's actually failing on
end
assert_redirected_to user_path(assigns(:user)) #it doesn't get here yet
end
The error it gives is "Can't find Account without ID" so I kind of understand WHY it's failing, because of the fact that it doesn't have the account object (or account_id as it were) to know under what account to create the user. I have tried variations of the following but I am completely lost:
post :create, :user => { accounts(:one) } #I have the 'fixtures :accounts' syntax at the top of the test class
post :create, [accounts(:one), :user] => { }
post :create, :user => { accounts(:one), #other params for :user }
and like I said, just about every variation I could think of. I can't find much documentation on doing this and this might be why people have moved to Factories for doing test data, but I want to understand things that come standard in Rails before moving onto other things.
Can anyone help me get this working?
© Stack Overflow or respective owner