Search code examples
ruby-on-railsfixtures

Rails fixtures for testing: selectively load individual records?


Do I have to put all fixture records for a model in the same file, named after the model? I would like to selectively load on some records depending on the test case, since the existing of the other data changes my app's behaviour


Solution

  • You do have to place all the fixture records in the same file - fixtures are placed before running tests, and are designed to be in the database for every test run. If you want to add test-specific fixtures, you're better off using factories, which provide localized, context-specific ways of creating and mocking objects for your tests to use. You can choose to just have them instantiated, or saved to the database - providing you with flexibility.

    The most popular factory for Rails is factory girl - you should give it a try!