0
0
Ruby on Railsframework~20 mins

Model tests in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Model Test Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Rails model test?
Given the following RSpec test for a Rails model, what will be the test result?
Ruby on Rails
RSpec.describe User, type: :model do
  it "is invalid without an email" do
    user = User.new(email: nil)
    expect(user).not_to be_valid
  end
end
AThe test raises a NoMethodError due to missing 'be_valid' matcher.
BThe test fails because the User model does not validate email presence.
CThe test passes because the User model requires an email.
DThe test is skipped because no subject is defined.
Attempts:
2 left
💡 Hint
Check if the User model has a validation for email presence.
state_output
intermediate
2:00remaining
What is the value of errors after validation fails?
Consider this Rails model test snippet. What will be the content of user.errors[:email] after validation?
Ruby on Rails
user = User.new(email: nil)
user.valid?
user.errors[:email]
A[] (empty array)
B["can't be blank"]
C["is invalid"]
Dnil
Attempts:
2 left
💡 Hint
What error message does Rails add for missing required fields?
📝 Syntax
advanced
2:00remaining
Which option correctly tests uniqueness validation in Rails model?
You want to test that the User model validates uniqueness of email. Which RSpec test code is correct?
Ait { should validate_uniqueness_of(:email) }
Bit { should validate_uniqueness_of_email }
Cit { expect(User).to validate_uniqueness_of(:email) }
Dit { should validate_uniqueness(:email) }
Attempts:
2 left
💡 Hint
Check the syntax of the shoulda-matchers gem for uniqueness validation.
🔧 Debug
advanced
2:00remaining
Why does this model test raise an error?
This test raises an error when run. What is the cause?
Ruby on Rails
RSpec.describe User, type: :model do
  it "validates presence of email" do
    user = User.new
    expect(user.valid?).to be_falsey
  end
end
AThe test raises an error because the User model lacks any validations.
BThe test raises an error because 'valid?' returns nil instead of boolean.
CThe test raises an error because 'be_falsey' is not a valid matcher.
DThe test does not raise an error; it runs and fails or passes depending on validations.
Attempts:
2 left
💡 Hint
Check if 'be_falsey' is a valid RSpec matcher and what 'valid?' returns.
🧠 Conceptual
expert
2:00remaining
What is the purpose of 'subject' in Rails model tests?
In RSpec model tests, what does defining 'subject { described_class.new }' achieve?
AIt defines the default object for test examples, simplifying test code.
BIt automatically runs validations before each test example.
CIt sets the database schema for the model during tests.
DIt mocks the model to avoid database calls during tests.
Attempts:
2 left
💡 Hint
Think about how 'subject' is used in RSpec to refer to the main object under test.