Jason
Jason

Reputation: 23395

Rails 2.3.2 - How to seed my test tables with data?

I am just learning Cucumber / RSpec, my application requires certain tables (e.g. categories, countries, price_list etc...) to be fully populated before it will really work properly. Some of these tables have over 500 rows (which are all required).

I was thinking of making MySQL dumps of the required tables & having a script import these to the test database before running each scenario, but this does not feel very efficient (and not sure how to kick off the import anyway).

Is there a best practice approach on how to accomplish this?

Any advice is very much appreciated!

Upvotes: 0

Views: 181

Answers (1)

austinfromboston
austinfromboston

Reputation: 3780

What you are looking for is test fixtures. I recommend the gem fixture_builder, for describing your fixtures in code which is then dumped to yaml which then goes into the database at the beginning of your test run.

I also recommend looking at ways to simplify your test data. While you may have 500 product codes in production, you should be able to test most special cases with a list of 15 or so products in test. Testing shipping to 2 countries is about as effective as testing shipping to 100 countries, etc. It's also much easier to keep track of.

Start with a small set of fixture data for tests and add data for setting up special cases as you need it, rather than putting your production database into your tests.

Upvotes: 1

Related Questions