0
0
RailsHow-ToBeginner · 3 min read

How to Use Fixtures in Rails for Test Data Setup

In Rails, fixtures are YAML files that preload test data into your test database before running tests. You use them by placing YAML files in the test/fixtures folder and referencing them in your test cases with fixtures :model_name. This lets you write tests using consistent, predefined data.
📐

Syntax

Fixtures are defined in YAML files named after your models, placed in test/fixtures. Each entry has a label and attributes for the record.

In your test file, use fixtures :model_name to load the fixture data. Access records by their labels as methods on the model.

ruby
# test/fixtures/users.yml
john:
  name: John Doe
  email: john@example.com

jane:
  name: Jane Smith
  email: jane@example.com

# test/models/user_test.rb
require 'test_helper'

class UserTest < ActiveSupport::TestCase
  fixtures :users

  test "user emails" do
    assert_equal "john@example.com", users(:john).email
    assert_equal "jane@example.com", users(:jane).email
  end
end
Output
Run options: --seed 12345 # Running: . Finished in 0.12345s, 8.1000 runs/s, 16.2000 assertions/s. 1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
💻

Example

This example shows how to define fixture data for a users table and use it in a test to check user emails.

ruby
# test/fixtures/users.yml
alice:
  name: Alice Wonderland
  email: alice@example.com

bob:
  name: Bob Builder
  email: bob@example.com

# test/models/user_test.rb
require 'test_helper'

class UserTest < ActiveSupport::TestCase
  fixtures :users

  test "check user emails" do
    assert_equal "alice@example.com", users(:alice).email
    assert_equal "bob@example.com", users(:bob).email
  end
end
Output
Run options: --seed 67890 # Running: . Finished in 0.09876s, 10.1200 runs/s, 20.2400 assertions/s. 1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
⚠️

Common Pitfalls

  • Not naming fixture files exactly after the model's table name causes loading errors.
  • Using invalid YAML syntax breaks fixture loading.
  • Forgetting to declare fixtures :model_name in your test means fixture data won't be loaded.
  • Modifying fixture data inside tests can cause unexpected results; treat fixtures as read-only.
ruby
# Wrong: fixture file named incorrectly
# test/fixtures/user.yml (should be users.yml)

# Wrong: missing fixtures declaration in test
class UserTest < ActiveSupport::TestCase
  test "email check" do
    # users(:john) will raise error because fixtures not loaded
    assert_equal "john@example.com", users(:john).email
  end
end

# Right: correct fixture file and declaration
# test/fixtures/users.yml
john:
  name: John Doe
  email: john@example.com

class UserTest < ActiveSupport::TestCase
  fixtures :users

  test "email check" do
    assert_equal "john@example.com", users(:john).email
  end
end
📊

Quick Reference

Use this cheat sheet to remember key points about Rails fixtures:

ActionSyntax / LocationNotes
Define fixture dataYAML files in test/fixtures/model_name.ymlUse labels as record keys
Load fixtures in testfixtures :model_namePlace in test class
Access fixture recordmodel_name(:label)Returns ActiveRecord object
Run testsrails testFixtures load automatically
AvoidModifying fixtures in testsFixtures should be read-only

Key Takeaways

Fixtures are YAML files placed in test/fixtures named after your models.
Declare fixtures in your test with fixtures :model_name to load data.
Access fixture records by label using model_name(:label) in tests.
Keep fixture data read-only to avoid test inconsistencies.
Ensure fixture files have correct YAML syntax and naming.