Overview
RSpec is testing tool for the Ruby programming language. Born under the banner of Behaviour-Driven Development, it is designed to make Test-Driven Development a productive and enjoyable experience with features like:
- a rich command line program (the rspec command)
- textual descriptions of examples and groups (rspec-core)
- flexible and customizable reporting
- extensible expectation language (rspec-expectations)
- built-in mocking/stubbing framework (rspec-mocks)
Documentation
RDoc
- http://rubydoc.info/gems/rspec-core
- http://rubydoc.info/gems/rspec-expectations
- http://rubydoc.info/gems/rspec-mocks
- http://rubydoc.info/gems/rspec-rails
Cucumber Features
RSpec-1.x
The RSpec Book
The RSpec Book will introduce you to RSpec, Cucumber, and a number of other tools that make up the Ruby BDD family. Replete with tutorials and practical examples, the RSpec Book will help you get your BDD on, taking you from executable requirements to working software that is clean, well tested, well documented, flexible and highly maintainable.
Get Started Now
$ gem install rspec
|
Start with a very simple example that expresses some basic desired behaviour. # bowling_spec.rb require 'bowling' describe Bowling, "#score" do it "returns 0 for all gutter game" do bowling = Bowling.new 20.times { bowling.hit(0) } bowling.score.should eq(0) end end Run the example and watch it fail. $ rspec bowling_spec.rb ./bowling_spec.rb:4: uninitialized constant Bowling |
Now write just enough code to make it pass. # bowling.rb class Bowling def hit(pins) end def score 0 end end
Run the example and bask in the joy that is green. $ rspec bowling_spec.rb --format nested Bowling#score returns 0 for all gutter game Finished in 0.007534 seconds 1 example, 0 failures |
Take very small steps
Don’t rush ahead with more code. Instead, add another example and let it guide you to what you have to do next. And don’t forget to take time to refactor your code before it gets messy. You should keep your code clean at every step of the way.
Take the first step now!
gem install rspec
