small medium large xlarge

The RSpec Book: Behaviour-Driven Development with RSpec, Cucumber, and Friends


Cover image for The RSpec Book

The RSpec Book

Behaviour-Driven Development with RSpec, Cucumber, and Friends


Behaviour-Driven Development (BDD) gives you the best of Test Driven Development, Domain Driven Design, and Acceptance Test Driven Planning
techniques, so you can create better software with self-documenting, executable tests that bring users and developers together with a common language.

Get the most out of BDD in Ruby with The RSpec Book, written by the lead developer of RSpec, David Chelimsky.

This book uses Rails 3, RSpec 2, Ruby 1.8.7, and Cucumber 0.9.2

Effective Testing with RSpec 3 is here.

Customer Reviews

Some authors would be satisfied with just writing the definitive guide for a technology. These folks go a step further, and show you insider tips that will keep your tests clean and maintainable.

- Ian Dees

Software Engineer

The second generation of tools for the XP generation explained by their creators and maintainers. Awesome, a must read.

- Marcus Ahvne

software developer, Valtech

The RSpec Book teaches you much more than how to use RSpec’s features; it teaches you how to write code the way the RSpec team does: patiently, and with great precision and clarity. There is something here for everyone: beginners are given plenty of gentle attention but there is some real meat for the more experienced reader to chew on, too.

- Matt Wynn

independent programmer and coach

This book covers the territory of writing great software, and the authors are your experienced guides. If you follow the map that they have drawn, you’ll learn to write only the code that you need, and you’ll write it simply and clearly. You’ll come home from this journey with some experiences that will have immediate and lasting effects on the code in your editor and the code yet to flow from your fingertips.

- Craig Demyanovich

, 8th Light, Inc.

The RSpec Book is a fantastic introduction to all things BDD. It goes much deeper than just testing to provide you with the right tools you need to fully embrace the ideas that the framework has baked in.

- Aaron Bedra

principal, Relevance Inc.

See All Reviews

About this Title

Pages: 450
Published: 2010-12-02
Release: P2.1 (2012-09-13)
ISBN: 978-1-93435-637-1

You’ll get started right away with RSpec 2 and Cucumber by developing a simple game, using Cucumber to express high-level requirements in language your customer understands, and RSpec to express more granular requirements that focus on the behavior of individual objects in the system. You’ll learn how to use test doubles (mocks and stubs) to control the environment and focus the RSpec examples on one object at a time, and how to customize RSpec to “speak” in the language of your domain.

You’ll develop Rails 3 applications and use companion tools such as Webrat and Selenium to express requirements for web applications both in memory and in the browser. And you’ll learn to specify Rails views, controllers, and models, each in complete isolation from the other.

Whether you’re developing applications, frameworks, or the libraries that power them, The RSpec Book will help you write better code, better tests, and deliver better software to happier users.


Caution! You’ve fallen for a trap. You’ve picked up this book thinking it was about RSpec. Fortunately, you decided to read the foreword. Good! That gives me the opportunity to tell you about the mistake you just made and possibly save you from an unexpected fate.

You see, this book isn’t about RSpec at all. Oh, RSpec is certainly mentioned. There are lots of examples of how to use it. There’s even a very detailed reference manual in Part III. But that’s all just part of an insidiously clever deception, because this book is not about RSpec.

Perhaps you thought you might read about Cucumber? After all, Part IV is named “Cucumber.” Oh, these authors are clever; God they are! They’ve littered this book with examples and details that tell you all about Cucumber in all its intricacies and all its copious fiddledy-bits.
There’s even a section on using it with Rails and Webrat and all the other gory things that you’ll need to become a Cucumber expert. But this book is not about Cucumber.

No. This book is not about RSpec. And this book is not about Cucumber. This book is about. . .

I’m not sure I should tell you. I mean, once the secret gets out, it’s liable to cause mayhem. If it ever got out who the audience for this book really is, if the masses learned of the diabolical plan being executed in their midst, I’m not sure our civilization would survive.

You see. . . (come closer, and cover this part with your hand so nobody else can see it). . . you see, this book is not for. . . (covered?). . . it’s not for Ruby programmers!

There, I’ve said it! Now don’t panic, and don’t drop the book— whatever you do, don’t drop the book! Hold on tight, and keep it covered. Don’t let anyone else see.

Yes, you see, this book is not about RSpec. It’s not about Cucumber, It’s not for Ruby programmers. This book is for. . . (covered again?). . . it’s for all programmers!

Keep a good tight grip. I know it’s hard. Don’t look around suspiciously. Don’t draw attention to yourself. Just try to stay calm, breathe normally, and keep reading.

Yes, all the code is in Ruby. Yes, all the examples use RSpec and Cucumber to one degree or another. Yes, if you read this book, you will learn RSpec, Cucumber, and things about Ruby and Rails and Webrat that you didn’t know before. No doubt about it. Remember, the best lies
are near-truths.

Here’s the thing. While you read this book, you will think you are learning about all those cool tools. You will think “Oh, cool, I’m learning RSpec and Cucumber.” But you will be learning something else at the same time! Something unexpected. Something unadvertised. Something,
perhaps, unwelcome.

As you read these pages, a hidden meme will creep into your mind—a meme of such potency and power that it is likely to change everything about the way you program. And not just how you program in Ruby! If you read this book, that meme will change the way you program in Java, C#, Python, or (oh, God, the thought) COBOL! This book will change the way you code—period!

Worse, you don’t have to be a Ruby programmer to be infected by this meme. As I said, these authors are clever. Their unholy plan is to infect all programmers with this meme. You see, they’ve cleverly constructed the Ruby code in this book so that it can be understood by (gasp) any programmer at all! I mean, this is worse than Fluoridation!

Any programmer who picks up this book will be infected by the meme. And the meme is subtle. And the meme is persistent. And the meme will have its way. And when it does, our industry will never be the same again. Are you willing to risk that?

What is this meme? What name shall we give it? The meme is legion! It’s not just Agile, though Agile is there. It’s not just TDD and BDD, though both are there. It’s not just Continuous Integration, Acceptance Test–Driven Development, Acceptance Test–Driven Planning, or even Extreme Programming, though all those things are present in the meme.

No, the meme is more than any one of those things. The meme is a synergistic witches brew of some of the most contagious and effective ideas of the past two decades. The meme is. . .

Dare I say it?

The meme is. . .

. . . Craftsmanship.

Robert C. Martin
(Uncle Bob)

Contents & Extracts

  • Getting Started with RSpec and Cucumber
    • Introduction
    • Hello
    • Describing Features
    • Automating Features with Cucumber
    • Describing Code with RSpec excerpt
    • Adding New Features
    • Specifying an Algorithm
    • Refactoring with Confidence
    • Feeding Back What We’ve Learned
  • Behaviour-Driven Development
    • The Case for BDD
    • Writing Software That Matters
  • RSpec
    • Code Examples excerpt
    • RSpec::Expectations
    • RSpec::Mocks
    • Tools And Integration
    • Extending RSpec
  • Cucumber
    • Intro to Cucumber
    • Cucumber Detail
  • Behaviour-Driven Rails
    • BDD in Rails
    • Cucumber with Rails excerpt
    • Simulating the Browser with Webrat
    • Automating the Browser with Webrat and Selenium
    • Rails Views
    • Rails Controllers
    • Rails Models


David Chelimsky is the lead developer/maintainer of RSpec, and has contributed to several other open source projects including Cucumber, Aruba, and Rails. He has been developing software for over a decade, including three years training and mentoring agile teams at Object Mentor. He is currently a Senior Software Engineer at DRW Trading Group in Chicago, IL. In his spare time, David likes to play guitar, travel, and speak something resembling Portuguese.

Dave Astels is the Director of Technology at and has been involved with software and computing for over 25 years, recently having spent several years working exclusively with Ruby and Rails. Dave wrote the article that prompted Steven Baker to start the RSpec project.

Zach Dennis is a co-founder and fellow human at Mutually Human Software, an expert custom software strategy and design consultancy in Grand Rapids, Michigan. He has been enjoying Ruby for nearly eight years and has contributed to several projects such as Ruby’s standard library documentation, Ruby on Rails, and RSpec. In his spare time, Zach loves spending time with his family, continuously learning, playing music, and running

Aslak Hellesøy is a Senior Software Engineer at DRW Trading Group in London. While contributing to this book he was the Chief Scientist of BEKK Consulting in Oslo. In 2003, after seven years of professional Java programming, he fell in love with Ruby. He has contributed to dozens of open source projects and is the founder of the Cucumber
project. Aslak likes to cook, ski, and travel.

Bryan Helmkamp maintains Webrat, a Ruby library to implement acceptance tests for web applications in an expressive and maintainable way, and is an active participant in the New York City Ruby community. Bryan is the CTO of Efficiency 2.0, a startup that helps people understand and reduce their energy use.

Dan North writes software and coaches teams and organizations in agile and lean methods. He believes that most problems that teams face are about communication and understanding, which is why he puts so much emphasis on “getting the words right.” In 2003–4 this led him to develop the ideas that would become Behaviour-Driven Development. He is delighted by the community that has grown up around RSpec and Cucumber, and especially the enthusiasm and dedication of their core contributors. Dan is currently a Senior Software Engineer at DRW Trading Group in London, where he gets to actually code again!