small medium large xlarge

Effective Testing with RSpec 3: Build Ruby Apps with Confidence


Cover image for Effective Testing with RSpec 3

Effective Testing with RSpec 3

Build Ruby Apps with Confidence


“Our tests are broken again!” “Why does the suite take so long to run?” “What value are we getting from these tests anyway?” Solve your testing problems by building and maintaining quality software with RSpec—the popular BDD-flavored Ruby testing framework. This definitive guide from RSpec’s lead developer shows you how to use RSpec to drive more maintainable designs, specify and document expected behavior, and prevent regressions during refactoring. Build a project using RSpec to design, describe, and test the behavior of your code. Whether you’re new to automated tests or have been using them for years, this book will help you write more effective tests.

Printed in full color.

Customer Reviews

Ruby embraces the old Lisp idea that you should “build up a language” to address
your problem, and RSpec carries this approach into the world of testing. But
RSpec is a big toolbox, and in order to harness its full power you need a guide.
This is that guide: the bridge you need to take you from writing tests, to expressing
your design requirements in code.

- Avdi Grimm

Author of "Confident Ruby" and "Exceptional Ruby" and Head Chef, RubyTapas

Effective Testing with RSpec 3 does a great job of explaining both the main features
of RSpec as well as its lesser known, powerful, and often overlooked features. It’s
an essential resource for Rubyists looking to learn RSpec, or for those who use it
every day. I’ve already started using it regularly as a resource.

- Noel Rappin

Author of "Rails 4 Test Prescriptions" and Director of Development, Table XI

Myron and Ian have written the essential “missing” manual for modern RSpec,
connecting all the latest best practices for TDD and BDD. If you were having
trouble mastering modern testing with RSpec, this book will sort you out.

- Sam Joseph

Co-Instructor on the BerkeleyX Agile Developing using Ruby on Rails MOOC and Co-Founder, AgileVentures Charity

Myron has been the driving force behind RSpec development for years. He is an
expert on testing practices and getting the most out of the RSpec suite. His care
and attention to detail are impeccable, and it shows in both the codebase and
this book.

- Xavier Shay

RSpec Core Team Member and Payments Engineering Lead, Square

Having worked with Myron for many years on the RSpec core team, I’m very
pleased to see Effective Testing with RSpec 3. This book contains everything you
need to learn not only how RSpec works, but how to test effectively with the
framework. If you’re working with RSpec on a daily basis and looking to get better
at working with the framework, I could not recommend this book more heartily.

- Sam Phippen

Rspec Core Team Member and Engineer, Digital Ocean

The exercises are illustrative and challenging. The final chapter on test doubles
really synthesizes the best way to use these tools. You’ll write far more readable
and robust specs after reading this book, especially when interfacing with the
shifting sands of third-party APIs.

- Nigel Lowry

Company Director and Principal Consultant, Lemmata

Effective Testing with RSpec 3 is well worth a read: it is much more than a technical
reference and will make you a better developer by teaching you how to write more
expressive, robust, and maintainable tests.

- Alessandro Bahgat

Professional Software Developer

See All Reviews

Choose Your Format(s)

  • $19.00 In Stock
  • Ebooks are DRM free.

  • Ebook delivery options.

What You Need

To follow along with the book, you’ll need Ruby 2.2+. The book will guide you through installing RSpec 3 and setting up a new project to use it.

Contents & Extracts


  • Getting Started
    • Getting Started With RSpec
      • Installing RSpec
      • Your First Spec
      • Understanding Failure
      • Sharing Setup (But Not Sandwiches)
      • Your Turn
    • From Writing Specs to Running Them excerpt
      • Customizing Your Specs’ Output
      • Identifying Slow Examples
      • Running Just What You Need
      • Marking Work in Progress
      • Your Turn
    • The RSpec Way
      • What Your Specs Are Doing for You
      • Comparing Costs and Benefits
      • Different Types of Specs
      • Guidelines
  • Building an App With RSpec 3
    • Starting On the Outside: Acceptance Specs
      • First Steps
      • Deciding What to Test First
      • Checking the Response
      • Filling In the Response Body
      • Querying the Data
      • Saving Your Progress: Pending Specs
      • Your Turn
    • Testing in Isolation: Unit Specs
      • From Acceptance Specs to Unit Specs
      • Filling In the First Spec
      • Handling Success
      • Refactoring
      • Handling Failure
      • Defining the Ledger
      • Your Turn
    • Getting Real: Integration Specs
      • Hooking Up the Database
      • Testing Ledger Behavior
      • Testing the Invalid Case excerpt
      • Isolating Your Specs Using Database Transactions
      • Filling In the Behavior
      • Querying Expenses
      • Ensuring the Application Works for Real
      • Your Turn
  • RSpec Core
    • Structuring Code Examples
      • Getting the Words Right
      • Sharing Common Logic
      • Sharing Example Groups
      • Your Turn
    • Slicing and Dicing Specs with Metadata
      • Defining Metadata
      • Reading Metadata
      • Selecting Which Specs to Run
      • Sharing Code Conditionally
      • Changing How Your Specs Run
      • Your Turn
    • Configuring RSpec
      • Command-Line Configuration
      • Setting Command-Line Defaults
      • Using a Custom Formatter
      • RSpec.configure
      • Your Turn
  • RSpec Expectations
    • Exploring RSpec Expectations
      • Parts of an Expectation
      • How Matchers Work
      • Composing Matchers
      • Generated Example Descriptions
      • Your Turn
    • Matchers Included in RSpec Expectations
      • Primitive Matchers
      • Higher-Order Matchers
      • Block Matchers
      • Your Turn
    • Creating Custom Matchers
      • Delegating to Existing Matchers Using Helper Methods
      • Defining Matcher Aliases
      • Negating Matchers
      • Using the Matcher DSL
      • Defining a Matcher Class
      • Your Turn
  • RSpec Mocks
    • Understanding Test Doubles
      • Types of Test Doubles
      • Usage Modes: Mocks, Stubs, and Spies
      • Origins: Pure, Partial, and Verifying Doubles excerpt
      • Your Turn
    • Customizing Test Doubles
      • Configuring Responses
      • Setting Constraints
      • Your Turn
    • Using Test Doubles Effectively
      • Constructing Your Test Environment
      • Stubject (Stubbing the Subject)
      • Using Partial Doubles Effectively
      • Connecting the Test Subject to Its Environment
      • The Risks of Mocking Third-Party Code
      • High-Fidelity Fakes
      • Faking I/O with StringIO
      • Wrapping a Third-Party Dependency
      • Your Turn


Myron Marston, a longtime Ruby programmer, has led the development of RSpec since 2012. He works as a senior software engineer at Moz in Seattle. Myron tweets as @myronmarston.

By day, Ian Dees slings code, tests, and puns at a Portland-area test equipment manufacturer. By night, he converts espresso into programming books, including Cucumber Recipes. Ian tweets as