gilesdotcodes
gilesdotcodes

Reputation: 158

How can I run tests on a pre-populated database in Rails?

So I am writing acceptance tests for a single feature of a large App. I needed a lot of data for this and have a lot of scenarios to test; so I have pre-populated a mysql database using Sequel Pro.

Obviously the appname_test database is in place for the other tests in the app. I would like to know how I could load up a .sql file (which is a sql dump of content) into this database at the start of my tests (before the first context statement).

I am new to development so this is completely new to me. Thanks in advance for any help.

Update:

I have used the yaml_db gem to dump the dev db (db:data:dump) and then load it into the test db (db:data:load ENV_RAILS=test). However, as soon as I run my specs the test db is wiped clean! Is there a way to run db:data:load ENV_RAILS=test from inside the spec file. I have tried:

require 'rake'
Rake::Task['bundle exec db:data:load ENV_RAILS=test'].invoke

but it says Don't know how to build task 'be db:data:load ENV_RAILS=test'

Upvotes: 0

Views: 457

Answers (1)

gilesdotcodes
gilesdotcodes

Reputation: 158

OK, so here is what I did to solve this.

I used the yaml_db gem and rake db:data:dump which creates db/data.yml (a dump of the dev db).

I then had to write a library and rake task which converted the data.yml file into individual fixtures files. I run this new rake task once to create the fixure files.

Then, at the start of my spec I call fixtures :all which populates the test database with all the fixtures.

Upvotes: 1

Related Questions