0
0
Ruby on Railsframework~20 mins

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

Choose your learning style9 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.