Easels App with Test Driven Development: Part 3
Master Test-Driven Development with Ruby on Rails
Ruby on Rails Testing Fundamentals
Test-Driven Development
Write tests before implementing functionality to ensure code quality and proper feature behavior. Build confidence in your application's reliability.
RSpec Feature Testing
Simulate user interactions with Capybara to test complete workflows. Verify that users can successfully complete tasks end-to-end.
Rails MVC Architecture
Understand how models, views, and controllers work together. Learn proper separation of concerns for maintainable applications.
Feature tests follow a consistent pattern: user authentication, task creation, specific action testing, and verification. This approach ensures comprehensive coverage of user workflows.
Complete Feature Implementation
Generate Feature Test
Create RSpec feature test file with rails g rspec:feature command and define test scenario
Add Database Migration
Create completed_at datetime field with AddCompletedToTodo migration to track completion timestamps
Update Model Logic
Define completed? method in Todo model that checks for presence of completed_at value
Create Controller Actions
Build CompletionsController with create action to handle marking todos as complete
Using nested routes like resources :todos do resource :completion creates logical URL structure that clearly associates completion actions with specific todo items.
Complete vs Incomplete Actions
| Feature | Mark Complete | Mark Incomplete |
|---|---|---|
| HTTP Method | POST | DELETE |
| Controller Action | create | destroy |
| Database Change | Set completed_at timestamp | Set completed_at to nil |
| Button Visibility | Shows when incomplete | Shows when completed |
The current implementation contains duplicate code between create and destroy methods. This violates DRY principles and should be refactored in the next exercise for better maintainability.
Implementation Verification
Users can mark todos as complete with proper database updates
Users can revert completion status by removing timestamp
Completed todos display with appropriate CSS class styling
Five total tests confirm feature reliability and integration
Key Takeaways