A fixture is a fixed set of sample data stored in YAML files that Rails loads into the test database before running tests. It helps tests run with known data.
Click to reveal answer
beginner
What is a factory in Rails testing?
A factory is a Ruby object that builds test data dynamically. It creates objects with default or customized attributes when tests run, often using Factory Bot.
Click to reveal answer
intermediate
How do fixtures and factories differ in flexibility?
Fixtures use static data loaded before tests, so they are less flexible. Factories create data on demand with customizable attributes, making them more flexible for varied test cases.
Click to reveal answer
beginner
How do you define a simple factory for a User model using Factory Bot?
You define it like this:
FactoryBot.define do
factory :user do
name { "Test User" }
email { "user@example.com" }
end
end
Click to reveal answer
intermediate
Why might you choose factories over fixtures in Rails tests?
Factories allow creating fresh, customized data for each test, reducing dependencies and making tests easier to maintain and understand. Fixtures can become brittle with static data.
Click to reveal answer
What file format do Rails fixtures use to store test data?
AYAML
BJSON
CXML
DCSV
✗ Incorrect
Rails fixtures are stored in YAML files, which are easy to read and write.
Which gem is commonly used to create factories in Rails?
AFactory Bot
BPry
CCapybara
DRSpec
✗ Incorrect
Factory Bot is the popular gem for defining and using factories in Rails tests.
Which is a benefit of using factories over fixtures?
AStatic data for all tests
BFaster test runs always
CDynamic, customizable test data
DNo need to write any code
✗ Incorrect
Factories create dynamic data with customizable attributes, unlike static fixtures.
How do you access fixture data in a Rails test?
AUsing instance variables
BUsing fixture names as methods
CUsing FactoryBot.create
DUsing SQL queries
✗ Incorrect
Fixtures are accessed by their names as methods inside tests, e.g., users(:one).
Which statement about fixtures is true?
AThey generate data at runtime dynamically
BThey cannot be used with Rails
CThey require Factory Bot to work
DThey are loaded once before tests run
✗ Incorrect
Fixtures load static data into the test database before tests run.
Explain the difference between fixtures and factories in Rails testing.
Think about how test data is created and used.
You got /4 concepts.
Describe how you would define and use a factory for a User model in a Rails test.
Focus on the syntax and usage steps.
You got /4 concepts.
Practice
(1/5)
1. What is the main difference between fixtures and factories in Rails testing?
easy
A. Fixtures use static YAML files, while factories generate data dynamically with Ruby code.
B. Fixtures are faster to write than factories.
C. Factories require no setup, but fixtures need database cleaning.
D. Factories store data in YAML files, fixtures use Ruby code.
Solution
Step 1: Understand fixtures data source
Fixtures load test data from static YAML files before tests run.
Step 2: Understand factories data generation
Factories create test data dynamically using Ruby code during test execution.
Final Answer:
Fixtures use static YAML files, while factories generate data dynamically with Ruby code. -> Option A