Skip to content

Instantly share code, notes, and snippets.

@gravitystorm
Created October 7, 2016 09:04
Show Gist options
  • Save gravitystorm/80dc264af80c6bcac7e0cb110ae32256 to your computer and use it in GitHub Desktop.
Save gravitystorm/80dc264af80c6bcac7e0cb110ae32256 to your computer and use it in GitHub Desktop.
Steps to replace fixtures with factories

These are instructions for replacing fixtures with factories, developed for https://github.com/openstreetmap/openstreetmap-website

  • Run the tests

Make sure all the tests pass (bundle exec rake test) before starting!

  • Choose the fixture

Pick a fixture file from test/fixtures. If they come in pairs (e.g. notes.yml and note_comments.yml) it's easier to do both at the same time. Have a read through, and get a rough idea of what the differences are between each of the fixtures.

  • Make a factory

Create a corresponding factory in test/factories. Use existing factories as a guide. Specify as few attributes as you can get away with. Don't specify anything that's automatic (e.g. created_at). Don't specify any attributes that can be left blank. If there's some text that's required (e.g. a username, title, body or comment) then use a sequence to easily make them all different.

  • Remove the fixture

Delete the fixture file from test/fixtures. Now log into your test database, and "DELETE FROM" the corresponding tables.

  • Remove the references to loading the fixtures

At the top of various files in test/models and test/controllers, there are lines saying fixtures :notes, :note_comments and so on. Remove any references to the fixtures that you've deleted. Also check the self.api_fixtures method in test/test_helper.rb and remove any references from there.

  • Run the tests

Now run the tests (bundle exec rake test) and get an idea of what files you need to tackle. There can be a lot of errors and failures but they are each fairly easy to fix.

  • Fix the tests, one by one.

Start with the model tests in question, and work through them replacing any reference to your fixture with a corresponding factory. Have a look at diary_entries or note and note_comments for examples of what it looks like after refactoring.

  • Watch out for negative tests

There are often 'negative tests' which are a bit tricky. This can be something like "test no results are found" which made sense when the db was pre-loaded with data from fixtures, but makes much less sense if the database contains no data from factories! Be very suspicious about test methods that passed even when the fixtures were first removed. If you are dealing with a negative test, make sure you load some appropriate data first (e.g. load an object with :status => hidden before the test that checks hidden results aren't returned).

  • Submit a pull request

Your pull request will be reviewed, and you might get some feedback on it. Add some more commits to your branch if necessary, and then your pull request will be merged by a maintainer.

  • Celebrate!

Well done! If you've got to this point you've made a substantial contribution to updating the openstreetmap-website software. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment