Test-Driving
with RSpec
and Cucumber
Monday, April 2, 12
Slide 2
Slide 2 text
@terronk
[email protected]
Lee Edwards
Lead Developer, SideTour
Monday, April 2, 12
Slide 3
Slide 3 text
Test-Driven Development
RED
GREEN
REFACTOR
Monday, April 2, 12
Slide 4
Slide 4 text
Test-Driven Development
Write a test
Watch it fail
Write the code
Watch it pass
Refactor,
Refactor!
RED
GREEN
REFACTOR
Monday, April 2, 12
Slide 5
Slide 5 text
Example 1
•Test-Driving Fibonacci with RSpec
Monday, April 2, 12
Slide 6
Slide 6 text
Ensures working code
Drives good design
Prevents regression
Documents behavior
Monday, April 2, 12
Slide 7
Slide 7 text
Working Code
Monday, April 2, 12
Slide 8
Slide 8 text
Monday, April 2, 12
Slide 9
Slide 9 text
Regressions
Monday, April 2, 12
Slide 10
Slide 10 text
Documentation
Monday, April 2, 12
Slide 11
Slide 11 text
Example 2
•Adding a new requirement to our
Fibonacci method
Monday, April 2, 12
Slide 12
Slide 12 text
Refactoring
•Changes to code guided by tests
•Behavior doesn’t change
•Tests stay green
Monday, April 2, 12
Slide 13
Slide 13 text
Example 3
•Refactoring our Fibonacci method
Monday, April 2, 12
Slide 14
Slide 14 text
Unit tests vs.
Integration tests
•Unit tests cover isolated behaviors
•Integration tests cover full features
•Unit tests mock or stub interfaces
•Integration tests validate interfaces
Monday, April 2, 12
Slide 15
Slide 15 text
Cucumber
•High-level integration testing for web
applications
•Uses a web browser driver or a web
browser simulator
•Natural language steps with custom
definitions
Monday, April 2, 12
Slide 16
Slide 16 text
Two Styles of Cukes
Verbose
Concise
Monday, April 2, 12
Slide 17
Slide 17 text
Verbose
Feature
Monday, April 2, 12
Slide 18
Slide 18 text
Step Definitions
Monday, April 2, 12
Slide 19
Slide 19 text
Concise Feature
Monday, April 2, 12
Slide 20
Slide 20 text
Example 4
•(example Cucumber feature)
•drill down into model specs
Monday, April 2, 12
Slide 21
Slide 21 text
Advanced TDD
• Mocking and stubbing
• let statements
• Factories and Fixtures
Monday, April 2, 12
Slide 22
Slide 22 text
How to Learn More
• Use it on your projects
• Contribute to Open Source
• Read a book
• Get an internship at a company that TDDs
Monday, April 2, 12
Slide 23
Slide 23 text
Reading
• Test-Driven Development By Example by
Kent Beck
• Growing Object-Oriented Software,
Guided By Tests by Steve Freeman
• Refactoring by Martin Fowler
• The RSpec Book by David Chelimsky
Monday, April 2, 12
Slide 24
Slide 24 text
Setting up Rails BDD
• Add to Gemfile:
• rspec-rails
• cucumber-rails
• jasmine
• bundle install
• rake db:test:prepare
• rails g cucumber:install
• rails g rspec:install
• jasmine init
Monday, April 2, 12
Slide 25
Slide 25 text
Unit Testing
• Ruby RSpec, Test::Unit
• Python PyUnit, unittest
• Java JUnit
• Javascript Jasmine
• iOS OCUnit, Cedar
• Android Robolectric
Monday, April 2, 12