Challenge - 5 Problems
Model Test Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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
Attempts:
2 left
💡 Hint
Check if the User model has a validation for email presence.
✗ Incorrect
The test expects the user to be invalid without an email. If the User model validates presence of email, the test passes.
❓ state_output
intermediate2: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]
Attempts:
2 left
💡 Hint
What error message does Rails add for missing required fields?
✗ Incorrect
When a presence validation fails, Rails adds "can't be blank" to the errors for that attribute.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
Check the syntax of the shoulda-matchers gem for uniqueness validation.
✗ Incorrect
The correct syntax uses 'validate_uniqueness_of' matcher with should.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Check if 'be_falsey' is a valid RSpec matcher and what 'valid?' returns.
✗ Incorrect
'be_falsey' is a valid matcher and 'valid?' returns true or false. The test runs without error.
🧠 Conceptual
expert2:00remaining
What is the purpose of 'subject' in Rails model tests?
In RSpec model tests, what does defining 'subject { described_class.new }' achieve?
Attempts:
2 left
💡 Hint
Think about how 'subject' is used in RSpec to refer to the main object under test.
✗ Incorrect
'subject' defines the main object tested, so you can write cleaner tests without repeating code.