Easels App with Test Driven Development: Part 1
Master Test-Driven Development with Ruby on Rails
A complete task management application using Test-Driven Development principles, where you write failing tests first, then implement the minimum code needed to make them pass.
TDD Development Approach
Test-First Philosophy
Write tests that fail until you create the bare minimum production code to make them pass. This approach acts as your personal assistant in developing reliable applications.
Cleaner, Flexible Code
TDD advocates maintain this approach creates more maintainable code that's easier to modify and extend over time.
TDD Development Cycle
Write Failing Test
Create a test that describes the functionality you want to implement. The test should fail initially since the feature doesn't exist yet.
Write Minimum Code
Implement just enough production code to make the test pass. Don't over-engineer or add unnecessary features.
Verify and Refactor
Run tests to ensure they pass, then refactor code while keeping tests green. This ensures functionality works as intended.
Rails ships with test-unit by default, but the framework is designed to work with any testing suite. We're using RSpec because it's more popular and has readable syntax.
Initial Setup Commands
Creates new Rails app without default test folder (-T flag)
Sets up database schema before writing tests
Essential Testing Gems
RSpec Rails
Rails-specific version of RSpec testing framework with readable syntax. More popular than Rails' default test-unit among developers.
Capybara
Simulated web browser that creates responsive tests mimicking user interaction. Enables behavior-driven development with understandable syntax.
Database Cleaner
Wipes and rebuilds test database between runs, ensuring tests don't contaminate each other with leftover data.
Rails allows loading gems only in specific environments. Test-related gems should only load in the test environment to keep development and production clean.
Setting Up RSpec
Install RSpec
Run 'rails g rspec:install' to create RSpec files and folders. This generates the spec directory and helper files.
Create Features Folder
Create 'features' folder in spec directory for high-level acceptance tests that verify general app functionality.
Generate Feature Test
Use 'rails g rspec:feature user_visits_homepage' to create the test file with basic markup structure.
Feature tests are high-level acceptance tests that verify your app's general features work. While you can test individual components, feature tests give confidence that all parts function together.
TDD Error-Driven Development
Missing Link Error
Capybara can't find 'Add a New To-Do' button
Undefined Path Error
new_todo_path method doesn't exist
Route Success
Resourceful routes provide needed paths
TDD involves slowly making progress, correcting errors one step at a time.
Key Takeaways

.