0
0
Ruby on Railsframework~10 mins

Fixture and factory usage in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Fixture and factory usage
Start Test Setup
Load Fixture Data
Use Fixture in Test
Run Test Assertions
End Test
Start Test Setup
Call Factory to Build Object
Use Factory Object in Test
Run Test Assertions
End Test
Tests start by either loading fixture data or calling a factory to create objects, then use these objects in tests before running assertions and ending.
Execution Sample
Ruby on Rails
test "user has valid email" do
  user = users(:one) # fixture
  assert user.email.include?('@')
end
This test uses a fixture named :one to get a user and checks if the email contains '@'.
Execution Table
StepActionData Loaded/CreatedTest VariableAssertion Result
1Load fixture users(:one)User fixture data with email 'user@example.com'user = users(:one)N/A
2Check if user.email includes '@'N/Auser.email = 'user@example.com'True
3Assert conditionN/Auser.email.include?('@')Pass
4Test endsN/AN/ATest Passed
💡 Test ends after assertion passes
Variable Tracker
VariableStartAfter Step 1After Step 2Final
usernilUser object from fixture with email 'user@example.com'SameSame
Key Moments - 2 Insights
Why do we use fixtures instead of creating objects manually in tests?
Fixtures provide predefined data loaded before tests run, so tests can use consistent data without setup code, as shown in step 1 of the execution_table.
How is a factory different from a fixture in tests?
A factory creates objects dynamically during the test run, unlike fixtures which load static data before tests, allowing more flexible and customized test data.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'user.email' after step 2?
A'user@example.com'
B'test@domain.com'
Cnil
D'noemail'
💡 Hint
Check the 'Test Variable' column at step 2 in the execution_table.
At which step does the test check if the email contains '@'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table for the condition check.
If we replaced the fixture with a factory that creates a user with email 'factory@user.com', what changes in the variable_tracker?
AThe 'user' variable would be nil throughout
BThe 'user' variable would have email 'user@example.com' after step 1
CThe 'user' variable would have email 'factory@user.com' after step 1
DNo change in 'user' variable
💡 Hint
Consider how factories create objects dynamically compared to fixtures.
Concept Snapshot
Fixtures load static test data before tests run.
Factories create test objects dynamically during tests.
Use fixtures for simple, consistent data.
Use factories for flexible, customized data.
In tests, access fixtures by name (e.g., users(:one)).
Factories are called as methods to build objects.
Full Transcript
In Rails testing, fixtures and factories help create test data. Fixtures are predefined data loaded before tests, accessible by names like users(:one). Factories build objects dynamically during tests, allowing more customization. Tests use these objects to check behavior, like verifying a user's email contains '@'. The execution flow starts with loading or creating data, then running assertions, and finally ending the test. Understanding when and how data is prepared helps write clear and reliable tests.