Answer the question
In order to leave comments, you need to log in
Using preset values in the database (rspec rails testing)?
Good afternoon, the following question arose:
How is the following task solved in rails:
There is a user, a user can have only one role. There are N possible types of roles.
Now there is a roles table with fields id, name
And, accordingly, users: id, name, role_id
In migrations, there is a migration that fills the roles table with preset values: id and name.
How do I configure rspec rails to have predefined values in the roles table when testing? should I write in seeds.rb? What happens if I need to add a new role when the database is already large and nothing will be lost: I think it's better to write a migration with logic than to push these values into seeds.rb? What is the rails way?
Answer the question
In order to leave comments, you need to log in
Correct in this case is seeds.rb and not migration, and not only for testing.
Only it is necessary to write it correctly so that it does not insert existing values. Then rake db:seed can be called at any time without any fear.
Discover the wonderful world of factories: https://github.com/thoughtbot/factory_girl/
They will help you solve almost all problems with creating objects for testing.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question