A
A
Andriy Stepanyuk2015-09-28 11:04:26
Ruby on Rails
Andriy Stepanyuk, 2015-09-28 11:04:26

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

2 answer(s)
M
malroc, 2015-09-28
@malroc

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.

E
Eugene Burmakin, 2015-09-28
@Freika

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 question

Ask a Question

731 491 924 answers to any question