Concept Flow - Model tests
Write model code
Write test cases
Run tests
Tests pass?
No→Fix code or tests
Yes
Model behavior verified
This flow shows writing model code, creating tests, running them, and fixing issues until tests pass.
class User < ApplicationRecord validates :email, presence: true end user = User.new assert user.valid? == false
| Step | Action | Input/State | Output/Result |
|---|---|---|---|
| 1 | Create User instance | User.new(email: nil) | User object with email=nil |
| 2 | Validate user | user.valid? | false (email missing) |
| 3 | Set email | user.email = 'test@example.com' | email set |
| 4 | Validate user again | user.valid? | true (email present) |
| 5 | Test assertion | assert user.valid? | passes |
| 6 | Run test suite | all tests | success |
| 7 | End | tests passed | Model behavior confirmed |
| Variable | Start | After Step 1 | After Step 3 | Final |
|---|---|---|---|---|
| user.email | nil | nil | 'test@example.com' | 'test@example.com' |
| user.valid? | N/A | false | true | true |
Model tests check if your data rules work. Write validations in model (e.g., presence). Write tests creating model instances. Run tests to confirm model behaves right. Fix code/tests if tests fail. Repeat until tests pass.