Recently I used Rspec to implement Test Driven Development(TDD) approach for developing an existing rails application. After setting up test environment it was time to migrate data from development database to test database in order to test some controller actions.
There are some neat ways to use 'factories' or 'fixtures' but that wasn't right choice due to size factor.
Taking dump of database and restoring into test db, well that's quick and easy solution but I didn't want data of all tables.
I was searching for a better solution and I found one. I used rails console to migrate data from development environment to test environment.
Here's what rails guide has to say about rails console:
The console command lets you interact with your Rails application from the command line. On the underside, rails console uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.
By default, 'rails console' connects with development database.
rails c production - connects with production database
rails c test - connects with test database
and most importantly, rails c --sandbox is great to use for testing out logic and experimenting with commands.
And now here is what I did to migrate data from development database to test database.
1 . Stored all data of a Model, say 'Product' to a variable @products.
2. Switched from development environment to test environment.
3. Last, Stored @products data to Product table of test database.
Hope you enjoyed and might find useful. Thanks and feel free to share your thoughts.