The Complete Guide to Rails Testing

Contains the solutions to your testing problems.

234-page Ebook (Download as PDF, Mobi, EPUB)

CGRT Slack access - chat with other Rails devs about testing

Lifetime access

The Complete Guide to Rails Testing

If you've tried to learn Rails testing, maybe you've been stuck on these common testing questions...

"What should I write tests for?"

You know you should be testing but you're not sure exactly what to test. The advice you've read doesn't seem to apply when the rubber meets the road.

How can I make testing a habitual part of my development workflow?

Maybe you conceptually understand how to write tests but you still aren't actually doing it. What's the difference between you and the people who write tests for all their code? Do they just have more discipline? What's the secret?

"What test framework should I use, MiniTest or RSpec?"

You know that MiniTest and RSpec are the two most popular testing frameworks for Rails, but you're not sure how to decide which one to use. There seem to be good (and strongly expressed!) arguments on both sides. How do you pick?

What level of test coverage do I need?

You've seen the tools that measure code coverage but you're not sure how much is really needed. 100%? 60%? 80%? Is there such a thing as too much testing?

Do I need to be doing TDD 100% of the time in order to be doing it "right"?

You've heard about test-driven development, and maybe you've even done a little TDD, but it's not clear how to do TDD all the time. Do you have to do TDD all the time in order to be a "real" tester? Is TDD even a good thing?

What are the various Rails testing tools and how do I use them?

Factory Bot, Faker, RSpec, VCR, WebMock... there are so many testing tools. What are they all for? Do you need them all?

Imagine what it would feel like to have really strong testing skills

You'd write tests as easily as you write regular code.

When you're comfortable with testing, you don't have to think hard about it. The tests just flow out. In fact, all your coding gets easier, because the tests you write help guide you forward.

You wouldn't have to force yourself to write tests because testing is just a habit.

To developers who are good at testing, writing tests isn't an extra chore. Writing tests isn't something they have to remember to do. It's just a normal part of the coding process. Furthermore, it's a fun and enjoyable part of the coding process.

You would choose your testing tools with confidence.

When you understand what the available testing tools are and why different teams choose different tools, it becomes easy to make the right choice for yourself. You can make the decision once and never worry about it again.

You would know how to do TDD and when to use it.

It would be 100% clear to you how to TDD any feature—IF you wanted to. And when you don't want to TDD a feature, you'll know why you're making that choice.

Introducing: the Complete Guide to Rails Testing

234 Pages, E-book Format

Contains the solutions to your testing problems

✅ What are the different kinds of Rails tests?

What are all the Rails testing tools and how do I use them?
✅ Which testing framework should I use (RSpec or Minitest)?
How can I make testing a habitual part of my development work?
✅ What level of test coverage should I shoot for?
How do I add tests to an existing Rails project?
✅ Much more

What's in the book

The different kinds of Rails tests
There are several different types of RSpec tests you can use. I show which ones to use and which ones to skip.

The various Rails testing tools and how to use them
Covers RSpec, Factory Bot, Capybara, VCR and WebMock.

How to decide which testing framework to use
Explains how to decide between the two big testing frameworks, RSpec and MiniTest.

How to make testing a habit
It's not enough to know how to write tests, you actually have to do it. I show you how to make testing a habit.

What level of test coverage to shoot for
Shows what level of test coverage to shoot for (hint: it's not a number).

How to add tests to an existing Rails project
Adding tests to an existing Rails project is a whole different ballgame from testing a greenfield app. I show you how.

How to set up a new Rails project for testing
I show you exactly how I set up all my new Rails projects.

How to decide when to do test-driven development
Are you testing "wrong" if you don't do TDD? I show you my approach to test-driven development.

Factory Bot
Installation, configuraiton, factory definitions, nested factories, traits, callbacks and transient attributes.

RSpec Syntax
Ruby blocks, let and let! vs. instance variables, describe and context, RSpec's DSL.

System Specs
When to write system specs, how to write system specs, a formula for writing system specs for most features.

Model Specs
What kinds of model specs to write and what kinds not to, the difference between testing implementation and behavior, much more.

Request Specs
What request specs are, the scenarios where request specs are useful vs. where they're not.

Mocks and Stubs
Test doubles, the difference between mocks and stubs, when to use mocks and stubs.

VCR and WebMock
Why to use VCR and WebMock, installation and configuration, how to use VCR and WebMock.

About The Author

Jason Swett is a developer, speaker, author of The Complete Guide to Rails Testing and host of The Rails with Jason podcast.

Jason enjoys helping other developers get better at programming through speaking and writing. 

He has spoken in several countries including the United States, India, The Netherlands, Bulgaria and Nigeria.

Jason lives in Sand Lake, Michigan with his wife and two kids.

Table of Contents

What's inside the book?

  • 1. Introduction
    • 1.1. About the book
    • 1.2. Who this book is for
    • 1.3. What's in this book
    • 1.4. How to use this book
  • 2. Fundamentals: Intro to Testing Principles
    • 2.1. Overview
    • 2.2. How to make testing a habit
    • 2.3. What level of test coverage to shoot for
    • 2.4. Test-driven development (TDD) vs. testing without TDD
  • 3. Fundamentals: Rails Testing Tools
    • 3.1. Overview
    • 3.2. RSpec vs. Minitest: which framework to learn
    • 3.3. Rails testing tools and how to use them
    • 3.4. The different kinds of RSpec tests and when to use each
    • 3.5. Adding tests to an existing project vs. starting fresh
  • 4. Fundamentals: Your First Practice Tests
    • 4.1. Overview
    • 4.2. Setting up a new Rails project for testing
    • 4.3. A Rails testing “hello world” using RSpec and Capybara
  • 5. Factory Bot: Introduction
    • 5.1. Overview
    • 5.2. What Factory Bot is
    • 5.3. Factories, fixtures and manual test data creation
  • 6. Factory Bot: Getting Started
    • 6.1. Installing Factory Bot
    • 6.2. Factory definitions
    • 6.3. Where to put factory definitions
    • 6.4. Factory Bot hello world
    • 6.5. Configuring Factory Bot with RSpec
  • 7. Factory Bot: Build Strategies and Faker
    • 7.1. Build strategies
    • 7.2. Using Factory Bot with Faker
  • 8. Factory Bot: Advanced Usage
    • 8.1. Introduction
    • 8.2. Nested factories
    • 8.3. Traits
    • 8.4. When to use traits versus nested factories
    • 8.5. Callbacks
    • 8.6. Transient attributes
  • 9. RSpec Syntax: Introduction
    • 9.1. RSpec Syntax: Introduction
  • 10. RSpec Syntax: The Structure of a Test
    • 10.1. RSpec Syntax: The Structure of a Test
    • 10.2. The purpose of each test phase
    • 10.3. A concrete example
  • 11. RSpec Syntax: Understanding Blocks
    • 11.1. RSpec Syntax: Understanding Blocks
    • 11.2. Writing our own block
    • 11.3. Why this example is meaningful
    • 11.4. Implementing the example
    • 11.5. Coming back to the controller example
  • 12. RSpec Syntax: Let, Let! and Instance Variables
    • 12.1. The purpose of let and the differences between let and instance variables
    • 12.2. How let works and the difference between let and let!
    • 12.3. Takeaways
  • 13. RSpec Syntax: The Parts of an RSpec Test
    • 13.1. What we're going to do in this chapter
    • 13.2. The example code
    • 13.3. Specs, examples, "example groups" and "it blocks"
    • 13.4. The expect keyword
    • 13.5. What's next
  • 14. RSpec Syntax: Build Your Own RSpec, Part 1
    • 14.1. What we're going to do
    • 14.2. Step 1: expect
    • 14.3. Organizing our files
    • 14.4. What's next
  • 15. RSpec Syntax: Build Your Own RSpec, Part 2
    • 15.1. The goal
    • 15.2. The implementation
  • 16. RSpec Syntax: Describe and Context
    • 16.1. RSpec Syntax: Describe and Context
    • 16.2. When I use describe
    • 16.3. When I use context
  • 17. System Specs: Introduction
    • 17.1. What this section will cover
    • 17.2. What system specs are
    • 17.3. When to write system specs
    • 17.4. The tools that support system specs
    • 17.5. Why I recommend against using Cucumber
    • 17.6. The difference between system specs and feature specs
  • 18. System Specs: Getting Started
    • 18.1. Installing Capybara and related tools
    • 18.2. RSpec + Capybara "hello world" revisited
    • 18.3. Logging the user in before tests
    • 18.4. Headless vs. not headless
  • 19. System Specs: Writing Specs for CRUD Features
    • 19.1. System Specs: Writing Specs for CRUD Features
    • 19.2. The three integration test cases I write for any Rails CRUD feature
    • 19.3. Creating a record with valid inputs
    • 19.4. Trying to create a record with invalid inputs
    • 19.5. Updating a record
  • 20. System Specs: Tutorial
    • 20.1. Tutorial overview
    • 20.2. Setting up our Rails project
    • 20.3. Writing our integration tests
  • 21. System Specs: Capybara's Domain-Specific Language (DSL)
    • 21.1. Navigation
    • 21.2. Clicking links and buttons
    • 21.3. Interacting with forms
    • 21.4. Finding content on the page
  • 22. System Specs: Simplifying Test Code with Page Objects
    • 22.1. The challenge of keeping test code clean
    • 22.2. What a Page Object is and when it's helpful
    • 22.3. A Page Object example
    • 22.4. The obscure test
  • 23. Model Specs: Introduction
    • 23.1. The purpose of model specs
    • 23.2. Meaningful model specs vs. pointless model specs
    • 23.3. Why I don't recommend Shoulda matchers
  • 24. Model Specs: Tutorial, Part One
    • 24.1. What a model is
    • 24.2. Why model specs are different from other types of specs
    • 24.3. The tutorial
  • 25. Model Specs: Tutorial, Part Two
    • 25.1. Prerequisites
    • 25.2. The tutorial
    • 25.3. Takeaways
    • 25.4. What's next
  • 26. Request Specs
    • 26.1. When I use request specs and when I don't
    • 26.2. What's next
  • 27. Mocks and Stubs
    • 27.1. Mocks and Stubs
    • 27.2. The problem with testing terminology
    • 27.3. Test doubles
    • 27.4. Stub explanation
    • 27.5. Mock explanation
    • 27.6. The difference between mocks and stubs
    • 27.7. Example application code
    • 27.8. Stub example
    • 27.9. Mock example
    • 27.10. Why I don't often use mocks or stubs in Rails
    • 27.11. What's next
  • 28. VCR and WebMock
    • 28.1. Introduction
    • 28.2. Using WebMock
    • 28.3. Using VCR
    • 28.4. Tutorial
    • 28.5. What we're going to do
    • 28.6. The feature
    • 28.7. Installing and Configuring WebMock
    • 28.8. Installing and Configuring VCR
    • 28.9. Takeaways
This book is now part of the Full Stack Rails Mastery Career Bundle.