10

My tests are written using fixtures and I am slowly refactoring them to use factories instead.

Once I've refactored a test class to not use fixtures I want to not load fixtures for that class. Is there a way to do this? Or am I stuck with either loading them for everything or nothing?

For context, here is how my fixtures are set up now:

class ActiveSupport::TestCase
   Rake::Task["db:fixtures:load"].execute
   ...
end
tirdadc
  • 4,603
  • 3
  • 38
  • 45
stoebelj
  • 1,536
  • 2
  • 14
  • 31

2 Answers2

8

Put fixtures in test/fixtures/users.yml

Then you are able to include them where you need, for example in spec_helper

# spec_helper.rb
fixtures :all   # include all fixtures
fixtures :users # include only specific fixtures

Doc: fixtures method

P.S. it works well together with FactoryBot gem (ex. FactoryGirl)

itsnikolay
  • 17,415
  • 4
  • 65
  • 64
3

You should be able to selectively load fixtures for a test case like this:

class ActiveSupport::TestCase
  ActiveRecord::FixtureSet.create_fixtures('test/fixtures', %w[units owners guests])
  ...
end
Jon M.
  • 3,483
  • 1
  • 20
  • 18
  • 2
    thanks for this! I have long moved on from this project and don't have time to verify that it works right now. If someone else having this question wants to verify it worked for them I will approve. – stoebelj Jun 29 '18 at 15:23