3

I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1 and capybara-1.1.2. I have this problem but I started thinking that maybe I'm doing something wrong... mostly about seeding data in the test database for testing purposes. Specifically, my issue is related to how to properly manage data in the test database when I have to test my application.

My doubt is: By seeding data (For Your Information: I use the ROOT_PATH/db/seed.rb file to inject that data) in the test database I'm doing things as they should be done? That is, how should I populate the test database since the data in that database* is required in order to make to properly work my application for testing purposes? Should I populate the test database at all?

In other words, what are best practices to handle database data in test mode (in my case)? And, generally speaking, how the situation should be handled?

***** For example, in order to work my application requires at least data related to an "anonymous" user, to "basic" articles, to "basic" article categories, etc.

Community
  • 1
  • 1
Backo
  • 18,291
  • 27
  • 103
  • 170

2 Answers2

1

You should use one of the following:

  1. Fixtures. See corresponding Rails documentation
  2. Factories. The most popular tool to created/manage factories is FactoryGirl. IMHO that's the best solution.
  3. Make sure data is seeded into test database. See this StackOverflow question.
Community
  • 1
  • 1
p0deje
  • 3,903
  • 1
  • 26
  • 37
  • (1), (2): Since the data that is required in order to make the application to properly work is needed throughout all the application, should I, for example using Cucumber, state/use the `Background` "functionality" in all/each `.feature` files/file so to load *factories*? Is that too tedious to make? If so, what are possible solution? (3): If I follow the (1)-(2) "approach" should I not seed data in the `test` database? – Backo Mar 19 '12 at 12:52
  • If you follow 1, 2 approach then you should not seed `test` database. If there is some absolutely needed piece of data, you should probably keep it in `Before` hook of Cucumber instead of Background. – p0deje Mar 19 '12 at 14:23
0

I had a similar problem, association made it necessary to have a bit of seed data:

  • Factories will make your tests really slow, they are perfectly OK for single objects, but not for a lot of seed data that has to be created for each test

  • Fixture Builder - http://github.com/rdy/fixture_builder I created a bunch of fixtures and just load them from the DB for each test, cut my test time down by 40%. You can also load the seeds file instead. But be careful, deleting or updating records will create unwanted side effects. Use factories for those specs.

  • Mock and Stub everything so that your tests rarely touch the DB. This has become very unpopular, you will eventually get passing specs that don't pick up on your actual errors.

Ray
  • 1,161
  • 7
  • 11