Home » Tools

RSpec – Behavior-Driven Development for Ruby

27 February 2012 No Comment

RSpec is an open source Behavior-Driven Development (BDD) ruby package that lets you build a specification alongside your software. BDD is an approach to software development that combines Test-Driven Development (TDD), Domain Driven Design and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.

You start by describing what you want, using #describe to define each methods passing the method’s name as the argument. For class method Rspec prefix a “.” to the name, and for instance level Rspec uses a “#” prefix. This is similar to standard Ruby documentation practices. You define the #context to explain the different scenarios in which the method could be executed. Each #context describes the state of the world before execution the method. You need a context for each execution path through a method. Finally you specify the behavior with the objective of having only one behavior specified in each example.

Given a file named “sample_spec.rb” with:
describe "something" do it "does something" do end end

When I run `rspec sample_spec.rb -fn`
Then the output should contain:
something does something

RSpec Best Practices

* Don’t begin example names with the word “should”.
* Use the right matcher: avoid # before with a double negative
* Use the “before :all” block carefully
* Do not create hundreds of records for a particular spec
* Do not over-mock
* Test the edge cases
* Stop “spec_helper” from being loaded multiple times
* Run RSpec to confirm readability
* Use “do..end” style multiline blocks for all blocks, even for one-line examples
* Create several test suites to speed up your workflow

RSpec Facts

Web Site: http://relishapp.com/rspec
Current version: 2.8 (February 2012)
License & Pricing: MIT License, open source / free
Support: Community
Documentation: https://www.relishapp.com/rspec/rspec-core

More RSpec Knowledge

RSpec Best Practices
Ruby for Newbies: Testing with RSpec
Behavior-driven testing with RSpec
Understanding RSpec Stories
An Introduction to RSpec
RSpec best practices
Rails Tutorial for Devise with RSpec and Cucumber
My top 7 RSpec best practices
Getting Started With IronRuby And RSpec
Testing Rails Engines With RSpec
RSpec best practices
Some RSpec Tips and Best Practices
An Introduction To RSpec
Ruby for Newbies: Testing with Rspec

RSpec Videos

RSpec Basics
Behavior Driven Development Using Ruby, Cucumber, and RSpec
RSpec and Cucumber Shared Helpers
Acceptance Testing with RSpec and Capybara
Efficient Rails Test-Driven Development (1st of 6 episodes)
Your tests are lying to you

Related Content:

Comments:

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.

*