Effective Testing with RSpec 3

Book description

"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.

RSpec has been downloaded more than 100 million times and has inspired countless test frameworks in other languages. Use this influential Ruby testing framework to iteratively develop a project with the confidence that comes from well-tested code. This book guides you through creating a Ruby project with RSpec, and explores the individual components in detail.

Start by learning the basics of installing and using RSpec. Then build a real-world JSON API, using RSpec throughout the process to drive a BDD-style outside-in workflow. Apply an effective test strategy to write fast, robust tests that support evolutionary design through refactoring. The rest of the book provides the definitive guide to RSpec's components. Use rspec-core's metadata to slice and dice your spec suite. Dig into rspec-expectations' matchers: compose them in flexible ways, specify expected outcomes with precision, and diagnose problems quickly with the help of good failure messages. Write fast, isolated tests with rspec-mocks' test doubles while pushing your code toward simpler interfaces.

The authors, with a combined 20 years of automated testing experience, share testing wisdom that will lead to a fun, productive testing experience.

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.

Publisher resources

View/Submit Errata

Table of contents

  1.  Foreword
  2.  Acknowledgments
    1. From Myron Marston
    2. From Ian Dees
  3.  Introduction
    1. How to Use This Book
    2. Code Snippets
    3. RSpec and Behavior-Driven Development
    4. Who We Are
    5. Who You Are
    6. A Note on Versions
    7. Online Resources
  4. Part I. Getting Started
    1. 1. Getting Started With RSpec
      1. Installing RSpec
      2. Your First Spec
      3. Understanding Failure
      4. Sharing Setup (But Not Sandwiches)
      5. Your Turn
    2. 2. From Writing Specs to Running Them
      1. Customizing Your Specs’ Output
      2. Identifying Slow Examples
      3. Running Just What You Need
      4. Marking Work in Progress
      5. Your Turn
    3. 3. The RSpec Way
      1. What Your Specs Are Doing for You
      2. Comparing Costs and Benefits
      3. Different Types of Specs
      4. Guidelines
  5. Part II. Building an App With RSpec 3
    1. 4. Starting On the Outside: Acceptance Specs
      1. First Steps
      2. Deciding What to Test First
      3. Checking the Response
      4. Filling In the Response Body
      5. Querying the Data
      6. Saving Your Progress: Pending Specs
      7. Your Turn
    2. 5. Testing in Isolation: Unit Specs
      1. From Acceptance Specs to Unit Specs
      2. Filling In the First Spec
      3. Handling Success
      4. Refactoring
      5. Handling Failure
      6. Defining the Ledger
      7. Your Turn
    3. 6. Getting Real: Integration Specs
      1. Hooking Up the Database
      2. Testing Ledger Behavior
      3. Testing the Invalid Case
      4. Isolating Your Specs Using Database Transactions
      5. Filling In the Behavior
      6. Querying Expenses
      7. Ensuring the Application Works for Real
      8. Your Turn
  6. Part III. RSpec Core
    1. 7. Structuring Code Examples
      1. Getting the Words Right
      2. Sharing Common Logic
      3. Sharing Example Groups
      4. Your Turn
    2. 8. Slicing and Dicing Specs with Metadata
      1. Defining Metadata
      2. Reading Metadata
      3. Selecting Which Specs to Run
      4. Sharing Code Conditionally
      5. Changing How Your Specs Run
      6. Your Turn
    3. 9. Configuring RSpec
      1. Command-Line Configuration
      2. Setting Command-Line Defaults
      3. Using a Custom Formatter
      4. RSpec.configure
      5. Your Turn
  7. Part IV. RSpec Expectations
    1. 10. Exploring RSpec Expectations
      1. Parts of an Expectation
      2. How Matchers Work
      3. Composing Matchers
      4. Generated Example Descriptions
      5. Your Turn
    2. 11. Matchers Included in RSpec Expectations
      1. Primitive Matchers
      2. Higher-Order Matchers
      3. Block Matchers
      4. Your Turn
    3. 12. Creating Custom Matchers
      1. Delegating to Existing Matchers Using Helper Methods
      2. Defining Matcher Aliases
      3. Negating Matchers
      4. Using the Matcher DSL
      5. Defining a Matcher Class
      6. Your Turn
  8. Part V. RSpec Mocks
    1. 13. Understanding Test Doubles
      1. Types of Test Doubles
      2. Usage Modes: Mocks, Stubs, and Spies
      3. Origins: Pure, Partial, and Verifying Doubles
      4. Your Turn
    2. 14. Customizing Test Doubles
      1. Configuring Responses
      2. Setting Constraints
      3. Your Turn
    3. 15. Using Test Doubles Effectively
      1. Constructing Your Test Environment
      2. Stubject (Stubbing the Subject)
      3. Using Partial Doubles Effectively
      4. Connecting the Test Subject to Its Environment
      5. The Risks of Mocking Third-Party Code
      6. High-Fidelity Fakes
      7. Faking I/O with StringIO
      8. Wrapping a Third-Party Dependency
      9. Your Turn
  9. A1. RSpec and the Wider Ruby Ecosystem
    1. Bundler
    2. Rake
    3. Using Parts of RSpec With Other Test Frameworks
  10. A2. Using RSpec with Rails
    1. Installation
    2. Using rspec-rails
    3. Spec Types
    4. Spec Types Cheat Sheet
    5. Rails Matchers Cheat Sheet
  11. A3. Matcher Cheat Sheet
    1. Value Matchers
    2. Block Matchers
  12. A4. Bibliography

Product information

  • Title: Effective Testing with RSpec 3
  • Author(s): Ian Dees, Myron Marston
  • Release date: August 2017
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680501988