small medium large xlarge

Rails 5 Test Prescriptions: Build a Healthy Codebase


Cover image for Rails 5 Test Prescriptions

Rails 5 Test Prescriptions

Build a Healthy Codebase


Does your Rails code suffer from bloat, brittleness, or inaccuracy? Cure these problems with the regular application of test-driven development. You’ll use Rails 5.1, Minitest 5, and RSpec 3.7, as well as popular testing libraries such as factory_bot and Cucumber. Updates include Rails 5.1 system tests and Webpack integration. Do what the doctor ordered to make your applications feel all better. Side effects may include better code, fewer bugs, and happier developers.

Choose Your Format(s)

  • $25.95 In Stock
    • Beta: What do I get?

  • Ebooks are DRM free.

  • Ebook delivery options.

  • The Paper Book will ship on 2018-02-10 (roughly).

About this Title

Pages: 375 (est)
Published: 2018-02-10
Release: B1.0 (2017-11-13)
ISBN: 978-1-68050-250-3

Your Ruby on Rails application is sick. Deadlines are looming, but every time you make the slightest change to the code, something else breaks. Nobody remembers what that tricky piece of code was supposed to do, and nobody can tell what it actually does. Plus, it has bugs. You need test-driven development: a process for improving the design, maintainability, and long-term viability of software.

With both practical code examples and discussion of why testing works, this book starts with the most basic features delivered as part of core Ruby on Rails. Once you’ve integrated those features into your coding practice, work with popular third-party testing tools such as RSpec, Jasmine, Cucumber, and factory_bot. Test the component parts of a Rails application, including the back-end model logic and the front-end display logic. With Rails examples, use testing to enable your code to respond better to future change. Plus, see how to handle real-world testing situations.

This new edition has been updated to Rails 5.1 and RSpec 3.7 and contains full coverage of new Rails features, including system tests and the Webpack-based JavaScript setup.

What You Need

Ruby 2.4, Rails 5.1

Contents & Extracts

This book is currently in beta, so the contents and extracts will change as the book is developed.

  • Preface
    • Who You Are
    • What’s In This Book
    • What You’ll Need
    • A Word About Tools, Best Practices, and Teaching TDD
    • Changes In This Edition
    • Online Resources
  • A Test-Driven Fable
    • Testing First Drives Design
    • What Is TDD Good For?
    • When TDD Needs Some Help
  • Test-Driven Development Basics
    • Infrastructure
    • The Requirements
    • Installing RSpec
    • Where to Start? excerpt
    • Running the Test
    • Making the Test Pass
    • The Second Test
    • Back on Task
    • Adding Some Math
    • The First Date
    • Using the Time Data
    • What You’ve Done
  • Test-Driven Rails
    • Let’s Write Some Rails
    • The Days Are Action-Packed
    • Who Controls the Controller?
    • A Test With a View
    • Testing for Failure
    • What You’ve Done
  • What Makes Great Tests
    • The Big One
    • Cost and Value
    • SWIFT: The Five Qualities of Valuable Tests
    • What You’ve Done
  • Testing Models excerpt
    • What Can You Do In a Model Test?
    • What Should I Test in a Model Test?
    • Okay, Funny Man, What Makes a Good Set of Model Tests?
    • Refactoring Models
    • A Note on Assertions per Test
    • Testing What Rails Gives You
    • Testing ActiveRecord Finders
    • Testing Shared Modules and ActiveSupport Concerns
    • Writing Your Own RSpec Matchers
    • What You’ve Done
  • Adding Data to Tests
    • What’s the Problem?
    • Fixtures
    • Factories
    • Dates and Times
    • What You’ve Done
  • Using Test Doubles as Mocks and Stubs
    • Test Doubles Defined
    • Creating Stubs
    • Mock Expectations
    • Using Mocks to Simulate Database Failure
    • Using Mocks to Specify Behavior
    • More Expectation Annotations
    • Mock Tips
    • What You’ve Done
  • Integration Testing with Capybara and Cucumber
    • A Field Guide to Integration Tests
    • Setting Up Capybara
    • Using Feature Tests to Build a Feature
    • What To Test In an RSpec System Test
    • Outside-In Testing
    • Making the Capybara Test Pass
    • Retrospective
    • Setting Up Cucumber
    • Writing Cucumber Features
    • Writing Cucumber Steps
    • Advanced Cucumber
    • Is Cucumber Worth It?
    • What You’ve Done
  • Testing JavaScript: Integration Testing
    • Integration Testing JavaScript With Capybara
    • Let’s Talk About Drivers
    • Making the Test Pass
    • Webpack in Developer Mode
    • What You’ve Done
  • Unit Testing JavaScript
    • Setting Up JavaScript Unit Tests
    • Writing a Sample Test
    • TDD in JavaScript
    • Jasmine Matchers
    • Testing Ajax Calls
    • Using testdouble.js
    • Connecting the JavaScript To the Server Code
    • What You’ve Done
  • Testing Rails Display Elements
    • Testing Routes
    • Testing Helper Methods excerpt
    • Testing Controllers and Requests
    • Simulating Requests
    • What to Expect in a Request Test
    • Older Rails Controller Tests
    • Testing Mailers
    • Testing Views and View Markup
    • Using Presenters
    • Testing Jobs and Cables
    • What You’ve Done
  • Minitest
    • Getting Started with Minitest
    • Minitest Basics
    • Running Minitest
    • Minitest Setup
    • Mocha
    • System Tests and Others
    • Minitest Helper Tests
    • Minitest and Routing
    • What You’ve Done
  • Testing for Security
    • User Authentication and Authorization
    • Adding Users and Roles
    • Restricting Access
    • More Access Control Testing
    • Using Roles
    • Protection Against Form Modification
    • Mass Assignment Testing
    • Other Security Resources
    • What You’ve Done
  • Testing External Services
    • External Testing Strategy
    • Our Service Integration Test
    • Introducing VCR
    • Client Unit Tests
    • Why an Adapter?
    • Adapter Tests
    • Testing for Error Cases
    • Smoke Tests and VCR Options
    • The World Is a Service
    • What You’ve Done
  • Troubleshooting and Debugging
    • General Principles
    • The Humble Print Statement
    • Git Bisect
    • RSpec or Minitest Bisect
    • Pry
    • Common Rails Gotchas
    • What You’ve Done
  • Running Tests Faster and Running Faster Tests
    • Running Smaller Groups of Tests
    • Running Rails in the Background
    • Guard
    • Writing Faster Tests by Bypassing Rails
    • Recommendations for Faster Tests
    • What You’ve Done
  • Testing Legacy Code
    • What’s a Legacy?
    • Set Expectations
    • Getting Started with Legacy Code
    • Test-Driven Exploration
    • Dependency Removal
    • Find the Seam
    • Don’t Look Back
    • What You’ve Done


Noel Rappin is the Director of Development at Table XI, and is the author of multiple technical books. Before joining Table XI, Noel ran internal training at Groupon, and has a Ph.D. in educational technology and user-centered design from the GVU Lab at the Georgia Institute of Technology. Find Noel online at