📖 Scenario: You are building a simple Rails app to manage books in a library. Each book has a title and an author. You want to make sure your model works correctly by writing tests.
🎯 Goal: Write model tests for the Book model to check that it validates presence of title and author.
📋 What You'll Learn
Create a Book model with title and author attributes
Add presence validations for title and author
Write model tests to check these validations using RSpec
💡 Why This Matters
🌍 Real World
Model tests help ensure your data rules work correctly before saving to the database, preventing bad data and bugs.
💼 Career
Writing model tests is a key skill for Rails developers to maintain reliable and maintainable applications.
Progress0 / 4 steps
1
Create the Book model with attributes
Create a Rails model called Book with string attributes title and author using a migration.
Ruby on Rails
Hint
Use rails generate model Book title:string author:string to create the model and migration.
2
Add presence validations to Book model
In the Book model, add presence validations for the title and author attributes using validates :title, presence: true and validates :author, presence: true.
Ruby on Rails
Hint
Use validates :attribute, presence: true for each attribute.
3
Create RSpec model test file for Book
Create a new RSpec test file spec/models/book_spec.rb with a describe Book block to hold your model tests.
Ruby on Rails
Hint
Use describe Book, type: :model do ... end to start the test file.
4
Write presence validation tests for Book model
Inside the describe Book block, write two tests using it blocks to check that title and author must be present. Use should validate_presence_of(:title) and should validate_presence_of(:author) matchers.
Ruby on Rails
Hint
Use it { should validate_presence_of(:attribute) } syntax from shoulda-matchers gem.
Practice
(1/5)
1. What is the main purpose of model tests in a Rails application?
easy
A. To check data validations and model methods work correctly
B. To test the user interface layout
C. To verify database server performance
D. To check network connectivity
Solution
Step 1: Understand what model tests cover
Model tests focus on the data layer, ensuring validations and methods behave as expected.
Step 2: Differentiate from other test types
User interface and network tests are handled by other test types, not model tests.
Final Answer:
To check data validations and model methods work correctly -> Option A
Quick Check:
Model tests = data rules and logic [OK]
Hint: Model tests check data rules and methods only [OK]
Common Mistakes:
Confusing model tests with UI tests
Thinking model tests check server or network
Assuming model tests check database performance
2. Which of the following is the correct syntax to test a presence validation on a Rails model attribute name using RSpec?
easy
A. expect(user).to validate_presence(:name)
B. expect(user).to be_validates_presence_of(:name)
C. expect(user).to have_presence_validation(:name)
D. expect(user).to validate_presence_of(:name)
Solution
Step 1: Recall RSpec validation matcher syntax
The correct matcher is validate_presence_of(:attribute) used with expect(model).
Step 2: Check each option for exact syntax
Only expect(user).to validate_presence_of(:name) matches the correct RSpec syntax for presence validation.
Final Answer:
expect(user).to validate_presence_of(:name) -> Option D
5. You want to test a custom method full_name in your User model that combines first_name and last_name. Which is the best way to write this model test?
hard
A. Write a controller test that calls full_name
B. Create a User with first and last names, then expect user.full_name to equal their combination
C. Check that full_name is validated for presence
D. Test that full_name is a database column
Solution
Step 1: Identify what to test for a custom method
Custom methods should be tested by calling them and checking their return values.
Step 2: Choose the test that verifies method output
Creating a User with names and expecting full_name to return combined string is correct.
Final Answer:
Create a User with first and last names, then expect user.full_name to equal their combination -> Option B
Quick Check:
Test method output by calling it with sample data [OK]
Hint: Test method output by calling it on sample model instance [OK]
Common Mistakes:
Testing method as if it were a database column
Confusing validation tests with method tests
Writing controller tests instead of model tests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.