Bird
Raised Fist0
Ruby on Railsframework~20 mins

Fixture and factory usage in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Fixture and Factory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What will be the user email after loading this fixture?
Given this YAML fixture for a User model, what will be the email of the user loaded in the test?
Ruby on Rails
users:
  one:
    id: 1
    name: "Alice"
    email: "alice@example.com"
  two:
    id: 2
    name: "Bob"
    email: "bob@example.com"
A"alice@example.com"
B"bob@example.com"
C"user@example.com"
Dnil
Attempts:
2 left
💡 Hint
Look at the fixture key used to load the user.
📝 Syntax
intermediate
2:00remaining
Which factory definition is correct for a User with a dynamic email?
Choose the correct FactoryBot factory definition that creates a User with a unique email each time.
A
factory :user do
  email { "user@example.com" }
  name { "Test User" }
end
B
factory :user do
  sequence(:email) { |n| "user#{n}@example.com" }
  name { "Test User" }
end
C
factory :user do
  email { generate(:email) }
  name { "Test User" }
end
D
factory :user do
  email { "user#{rand(100)}@example.com" }
  name { "Test User" }
end
Attempts:
2 left
💡 Hint
Look for the FactoryBot sequence syntax.
🔧 Debug
advanced
3:00remaining
Why does this test fail when using fixtures with associations?
Given these fixtures, why does the test fail with a 'nil' association error? users.yml: alice: id: 1 name: "Alice" posts.yml: first_post: id: 1 title: "Hello" user_id: 2
AThe post fixture should use 'user' instead of 'user_id' for association.
BThe fixtures are missing the 'created_at' timestamp causing failure.
CThe post fixture references a user_id (2) that does not exist in users.yml.
DThe user fixture 'alice' is not loaded because of a syntax error.
Attempts:
2 left
💡 Hint
Check the user_id value in posts.yml against users.yml ids.
❓ state_output
advanced
2:00remaining
What is the count of users after running this FactoryBot create sequence?
If you run this code in a test: 5.times { FactoryBot.create(:user) } What will User.count be?
A5
B1
C0
DError due to duplicate emails
Attempts:
2 left
💡 Hint
Assuming the factory uses sequences for unique emails.
🧠 Conceptual
expert
3:00remaining
Why prefer factories over fixtures in modern Rails testing?
Which reason best explains why many Rails developers prefer FactoryBot factories over fixtures?
AFixtures load faster than factories, making tests run quicker.
BFactories require less code and no configuration compared to fixtures.
CFixtures automatically handle complex associations better than factories.
DFactories allow dynamic, flexible data creation with traits and sequences, reducing brittle tests.
Attempts:
2 left
💡 Hint
Think about flexibility and test maintenance.

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

  1. Step 1: Understand fixtures data source

    Fixtures load test data from static YAML files before tests run.
  2. Step 2: Understand factories data generation

    Factories create test data dynamically using Ruby code during test execution.
  3. Final Answer:

    Fixtures use static YAML files, while factories generate data dynamically with Ruby code. -> Option A
  4. Quick Check:

    Fixtures = YAML, Factories = Ruby code [OK]
Hint: Fixtures = YAML files, factories = Ruby code [OK]
Common Mistakes:
  • Confusing fixtures as dynamic data generators
  • Thinking factories use YAML files
  • Assuming fixtures are created with Ruby code
2. Which of the following is the correct syntax to define a factory for a User model using FactoryBot?
easy
A. factory :user do name { "Alice" } end
B. define_factory :user do name "Alice" end
C. FactoryBot.define do factory :user do name { "Alice" } end end
D. factory :user { name: "Alice" }

Solution

  1. Step 1: Recognize FactoryBot syntax

    FactoryBot factories are defined inside a block passed to FactoryBot.define.
  2. Step 2: Check block and attribute syntax

    Attributes use curly braces with Ruby code, e.g., name { "Alice" }.
  3. Final Answer:

    FactoryBot.define do factory :user do name { "Alice" } end end -> Option C
  4. Quick Check:

    FactoryBot.define + block + attribute with braces [OK]
Hint: FactoryBot.define block wraps factory definitions [OK]
Common Mistakes:
  • Omitting FactoryBot.define block
  • Using wrong attribute syntax without braces
  • Using invalid factory syntax like curly braces for block
3. Given the following factory and test code, what will user.name output?
FactoryBot.define do
  factory :user do
    name { "Bob" }
  end
end

user = FactoryBot.create(:user)
medium
A. "Alice"
B. nil
C. Raises an error because name is missing
D. "Bob"

Solution

  1. Step 1: Understand factory attribute assignment

    The factory sets the name attribute to "Bob" using a block.
  2. Step 2: FactoryBot.create builds and saves user

    Calling create(:user) builds a user with name "Bob" and saves it.
  3. Final Answer:

    "Bob" -> Option D
  4. Quick Check:

    Factory attribute value = "Bob" [OK]
Hint: Factory attribute blocks set values directly [OK]
Common Mistakes:
  • Assuming default name is nil
  • Confusing create with build (create saves)
  • Expecting name to be "Alice" without definition
4. You have a fixture file users.yml with a user named john. In your test, you write users(:john) but get an error. What is a likely cause?
medium
A. The test database is not migrated.
B. The fixture file is not loaded or declared in the test setup.
C. You must use FactoryBot.create instead of fixtures.
D. The user named john is misspelled in the fixture file.

Solution

  1. Step 1: Check fixture loading in test

    Fixtures must be declared or loaded for the test to access them.
  2. Step 2: Confirm fixture file presence and naming

    If the fixture file exists but is not loaded, users(:john) will raise an error.
  3. Final Answer:

    The fixture file is not loaded or declared in the test setup. -> Option B
  4. Quick Check:

    Fixtures must be loaded to use named references [OK]
Hint: Load fixtures before using named references [OK]
Common Mistakes:
  • Assuming misspelling without checking loading
  • Thinking FactoryBot is required instead of fixtures
  • Ignoring fixture loading setup in test
5. You want to create multiple users with different emails using factories in a test. Which approach correctly creates 3 users with unique emails?
hard
A. factory :user do sequence(:email) { |n| "user#{n}@example.com" } end users = 3.times.map { FactoryBot.create(:user) }
B. factory :user do email "user@example.com" end users = 3.times.map { FactoryBot.create(:user) }
C. factory :user do email { "user@example.com" } end users = FactoryBot.create_list(:user, 3)
D. factory :user do email { "user@example.com" } end users = FactoryBot.build(:user, 3)

Solution

  1. Step 1: Understand sequence usage in factories

    Using sequence(:email) generates unique emails for each created user.
  2. Step 2: Confirm multiple user creation with unique emails

    Calling FactoryBot.create inside a loop with sequence ensures 3 users with different emails.
  3. Final Answer:

    factory :user do sequence(:email) { |n| "user#{n}@example.com" } end users = 3.times.map { FactoryBot.create(:user) } -> Option A
  4. Quick Check:

    Sequence + create loop = unique users [OK]
Hint: Use sequence for unique attributes in factories [OK]
Common Mistakes:
  • Using static email causing duplicates
  • Using build instead of create (no DB save)
  • Not using sequence for unique values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard