Test does not load many to many fixture
-
19-09-2019 - |
Question
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?
Solution
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.