Fixtures in Ruby on Rails with RSpec

Gokul
2 min readJan 28, 2023

What?

Ruby on Rails fixtures are a way to provide test data for your application. Most of the time, you use them with the RSpec testing framework to give your tests test data. Fixtures are a way to pre-populate your test database with a set of data that you can use to test your application.

Fixtures are stored in files that have a .yml file extension and are located in the test/fixtures directory of your Rails application. These files use the YAML (YAML Ain't Markup Language) format, which is a human-readable data serialization format. Each file is linked to a specific table in the database, and the data in the file is linked to the rows that will be added to that table.

How?

Here’s an example of how you might use fixtures in a Ruby on Rails application:

Create a fixture file in the test/fixtures directory. For example, if you want to test a model called User, you would create a file called users.yml in the test/fixtures directory.

# test/fixtures/users.yml

joe:
first_name: Joe
last_name: Smith
email: joe@example.com
password: secret

susan:
first_name: Susan
last_name: Johnson
email: susan@example.com
password: supersecret

Create a test for the User model. For example, you might create a file called…

--

--

Gokul

Consultant | Freelancer | Ruby on Rails | ReactJS