Does your Rails code suffer from bloat, brittleness, or inaccuracy? Cure these problems with the regular application of test-driven development. Rails 4 Test Prescriptions is a comprehensive guide to how tests can help you design and write better Rails applications. In this completely revised edition, you’ll learn why testing works and how to test effectively using Rails 4, Minitest 5, and RSpec 3, as well as popular testing libraries such as factory_girl and Cucumber. Do what the doctor ordered to make your applications feel all better. Side effects may include better code, fewer bugs, and happier developers.

Buy Now

Select a DRM-free Format:

In Stock
In Stock
Buy the eBook and get these DRM-free formats delivered immediately:
  • epub (for iPhone/iPad, Android, eReaders)
  • mobi (for Kindle)
  • PDF
We can automatically send them to your Kindle, and your Dropbox. (You'll need to log in to enable these options.)

This title is currently available in Beta. Buy the eBook now, and you'll be able to download successive releases of the eBook as the authors add material and correct mistakes. You'll get the final eBook when the book is finished.

If you buy the combo pack (Beta eBook + finished Paper Book) now, you'll get the Beta eBook immediately. You'll get the finished paper book when it's released (we're currently estimating 2014-10-15, but these dates can change).


About this Book

  • 350 pages (est)
  • Published:
  • Release: B2.0 (2014-07-16)
  • ISBN: 978-1-94122-219-5

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.

Containing 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, you’ll learn how to use popular third-party testing tools such as RSpec, Mocha, Cucumber, and factory_girl. You’ll test the component parts of a Rails application, including the back-end model logic and the front-end display logic. Using Rails examples, you’ll learn how to use testing to enable your code to respond better to future change. Plus, you’ll see how to handle real-world testing situations.

This completely revised edition contains a new tutorial, as well as new examples throughout the book. Many chapters, including the JavaScript chapter, have undergone major changes to reflect new tools and new practices. And there are brand new chapters on testing for security, and testing external services.

What You Need

Ruby 2.1, Rails 4

Contents and Extracts

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

  • A Testing Fable
    • Who You Are
    • The Power of Testing First
    • What Is TDD Good For?
    • When TDD Needs Some Help
    • Words to Live By
    • A Word About Tools, Best Practices, and Teaching TDD
    • Coming Up Next
    • Changes for the Second Edition
  • Test-Driven Development Basics excerpt
    • Infrastructure
    • The Requirements
    • Where to Start?
    • Running Our test
    • Passing Our Test
    • The Second Test
    • Our First Date
    • Using the Time Data
    • What We’ve Done
  • Test-Driven Rails
    • And Now, Let’s Write Some Rails
    • The Days Are Action Packed
    • Who Controls the Controller?
    • A Test With A View
    • What Have We Done? And What’s Next?
  • What Makes Great Tests
    • The Big One
    • The Big Two
    • The More Detailed Five: SWIFT Tests
  • Testing Models
    • What Can We 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
    • Modeling Data
  • Adding Data To Tests
    • What’s the Problem?
    • Fixtures
    • Factories
    • Dates and Times
    • Summary
  • Using Mock Objects
    • Mock Objects Defined
    • Installing Mocha
    • Creating Stubs in Mocha
    • Mock Expectations
    • Using Mocks To Simulate Rails Save
    • Using Mocks To Specify Behavior
    • More Expectation Annotations
    • Mock Tips
    • Coming Up Next
  • Testing Controllers And Views excerpt
    • Testing Controllers
    • Simulating Requests in a Controller Test
    • Evaluating Controller Results
    • Testing Routes
    • View Tests
    • Testing Helper Methods
    • Testing View Markup
    • Presenters
    • Testing Mailers
    • Managing Controller and View Tests
  • Driving Behavior With RSpec
    • Getting Started With RSpec
    • RSpec in 10 Minutes
    • The “It” Factor
    • Great “Expect”ations
    • Mocking RSpec
    • Let RSpec Eat Cake
    • RSpec On Rails
    • RSpec and Models
    • Controllers in RSpec
    • RSpec and Views
    • RSpec Routing Tests
    • RSpec and Helpers
    • Write Your Own RSpec Matchers
    • RSpec and Sharing
    • Data About Metadata
    • What’s Next
  • Integration Testing with Capybara and Cucumber
    • What to Test in an Integration Test
    • Setting up Capybara
    • Outside in Testing
    • Using Capybara
    • Making the Capybara Test Pass
    • Retrospective
    • Cucumber
    • Setting Up Cucumber
    • Writing Cucumber Features
    • Writing Cucumber Steps
    • More Advanced Cucumber
    • Is Cucumber Worth It?
    • Looking Ahead
  • Testing JavaScript
    • Unit Testing JavaScript
    • Integration Testing With JavaScript
  • Testing External Services
  • Testing For Security
    • User Authentication and Authorization
  • Troubleshooting and Debugging
  • Improving Your Test Environment
    • Running Smaller Groups of Tests
    • Making Faster Tests
  • Testing Legacy Code

About the Author

Noel Rappin is a senior developer and agile coach at Table XI, and 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

Upcoming Author Events

  • 2014-08-21: Noel Rappin
    (All Day) D is for Driven: Using Testing to Write Better Rails Applications -- full day workshop (Madison Ruby, Madison WI)

Latest Topics in the Forums…

Join the Discussion