Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails fixtures vs seeds

Tags:

I'm learning Ruby on Rails, and I'd like to just make sure I understand the difference between fixtures and seed data.

What I understand is that fixtures are basically test data that you run your testing assertions against, and have no persistence as soon as your tests are done, whereas seeds you put into the database automatically when you do something like rake db:seed.

Why use seeds in this case? Just to avoid having to write out all the myriad testing assertions? For data that you know will need to be in the database when the app is brought into production?

(I guess, static data that would always have been there like the first admin on a message board?)

like image 261
user2258651 Avatar asked Mar 19 '15 05:03

user2258651


People also ask

What is a rails fixture?

Fixtures are data that you can feed into your unit testing. They are automatically created whenever rails generates the corresponding tests for your controllers and models. They are only used for your tests and cannot actually be accessed when running the application.

What are seeds in rails?

What is Seed in Rails ? A useful way of populating a database with the initial data needed for a Rails project. This will allow us to populate the database in within our Rails Web application.

What does DB seed do?

Organizing seed files in a specific structure within a project's db/seeds/ directory enables Seedbank to either run all of the seed files for the current environment using the same rails db:seed task as vanilla Rails or to run a specific subset of tasks by specifying a seed file or environment name when running the ...


1 Answers

You got it right. Seed data is to populate the database with prerequisite data needed to allow the app to be usable from the beginning, like to add a default admin account which is required for the app to be managable from the start.

Seed data should not be used for testing purpose, while testing you should always make sure the database is clean so that you know that the only data used by an example is the data populated by the example it self not from any where outside, this allows to avoid confusion.

fixtures is one way of doing this. but a better way is to use factories like factory girl, check this railscast episode for a better explanation.

like image 168
Nafaa Boutefer Avatar answered Sep 30 '22 07:09

Nafaa Boutefer