Skip to content

Using Fixtures in RSpec

Ngan Pham edited this page Feb 24, 2026 · 1 revision

Using Fixtures in RSpec

Setup

# spec/rails_helper.rb
require "fixture_kit/rspec"

RSpec.configure do |config|
  config.use_transactional_fixtures = true
end

The RSpec entrypoint sets:

  • fixture_path default to spec/fixture_kit
  • adapter to FixtureKit::RSpecAdapter

Declare and Use

RSpec.describe Project do
  fixture "project_management"

  it "loads data" do
    expect(fixture.project).to be_present
  end
end

Lifecycle Timing

For a group that declares fixture:

  1. Declaration is registered when the example group is defined.
  2. Cache generation runs in prepend_before(:context) for that group.
  3. Cache mount runs in prepend_before(:example) and assigns repository to @_fixture_kit_repository.

Runner start (FixtureKit.runner.start) runs once in before(:suite) and clears cache path unless preserve-cache is enabled.

Metadata Key

FixtureKit stores the declaration on group metadata key :fixture_kit_declaration.

Common Gotcha

If a spec doesn’t declare fixture and still calls fixture, FixtureKit raises:

No fixture declared for this example group. Use `fixture "name"` in your describe/context block.

Clone this wiki locally