views:

225

answers:

1

I am using Test to test my application. I have some fixtures: users.yml, roles.yml, roles_users.yml. The users and roles are loaded but not the many to many table roles_users so the users are not related to any role. With Rails console in development everything is ok, in test any user is not connected to any role. I can see the users and roles in the test database but roles_users is empty

Do I have to specify somewhere how to load this fixture?

+2  A: 

If you're using Rails >=2.2 and a standard HABTM association between users and roles, you shouldn't need the roles_users.yml file. Instead, add a roles line for each user in users.yml:

sally:
  roles: admin, editor
  ...

fred:
  roles: basic
  ...

The values are the names of your role fixtures. I'm not completely sure this will solve your problem, but it's cleaner at the very least.

Alex Reisner
Yes, Rails 2.4 is not loading the HABTM. However I was able to force that behaviour in my environment.rb using;ENV['FIXTURES'] ||= 'roles_users'
rtacconi
When using your solution the roles_users table instead of having:user_id = 1, role_id = 1has:user_id = 1232342, role_id = 1Why user_id has that weired id?The test fails because role_id 1 in not connected to user_id 1
rtacconi
When using fixtures in Rails, the accepted practice is to NOT assign an explicit ID to each object, but let Rails do it for you. It will base the ID on the name of the fixture so you can load a fixture by name in a test (eg: `users(:fred)`, or `roles(:admin)`). You will end up with some long numbers for IDs, but you never have to know them--you can use fixture names instead of IDs in your tests, which makes them easier to read.
Alex Reisner
Thanks Alex, another guy said that and I was the main problem. They told me to use factory_girl
rtacconi