Leanpub Header

Skip to main content

Everyday Rails Testing with RSpec

A practical approach to test-driven development

Real-world advice for adding reliable tests to your Rails apps with RSpec, complete with expanded, exclusive content and a full sample application. Updates for Rails 7.1 in progress! Learn to test with confidence!

Minimum price

$19.00

$39.00

You pay

$39.00

Author earns

$31.20
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
7,279
Readers
164
Pages
35,954Words
About

About

About the Book

Built a couple of Rails applications, but struggling with reliable test coverage? Does your application testing consist of clicking around in the browser, hoping you catch everything before your customers do?

It's a common experience for new Ruby on Rails developers! Testing and test-driven development are crucial, but many tutorials don't delve deeply into these components. In Everyday Rails Testing with RSpec, you'll learn tried-and-true techniques for writing clear, effective, maintainable tests in RSpec—in turn, building more trustworthy features for your customers, and saving countless hours refreshing your browser.

Author

About the Author

Aaron Sumner

Aaron Sumner is a software developer and educator who has helped thousands of programmers level up their software engineering skills. In his blog, Everyday Rails, Aaron shows how he leverages tools from the Ruby ecosystem to be a productive developer, even when time and other resources are tight.

Leanpub Podcast

Episode 154

An Interview with Aaron Sumner

Translations

Translations

Languages

Contents

Table of Contents

Preface to this edition

Acknowledgements

1. Introduction

  1. Confident testing
  2. Why RSpec?
  3. Who should read this book
  4. My testing philosophy
  5. How the book is organized
  6. Downloading the sample code
  7. Code conventions
  8. Discussion and errata
  9. A note about gem versions
  10. A note about styling
  11. About the sample application

2. Setting up RSpec

  1. Dependencies
  2. Test database
  3. RSpec configuration
  4. The rspec binstub
  5. Try it out!
  6. Summary
  7. Exercises

3. Model specs

  1. Anatomy of a model spec
  2. Creating a model spec
  3. The RSpec syntax
  4. Testing validations
  5. Testing instance methods
  6. Testing class methods and scopes
  7. Testing all the cases
  8. More about matchers
  9. Summary
  10. Exercises

4. DRY enough specs

  1. Describing contexts in RSpec
  2. DRYer test setup and teardown
  3. How DRY is too DRY?
  4. Summary
  5. Exercises

5. Creating meaningful test data

  1. Factories or fixtures?
  2. Installing factory_bot
  3. Adding factories to the application
  4. Generating unique data with sequences
  5. Associations in factories
  6. Using factories safely
  7. Summary
  8. Exercises

6. Testing controllers

  1. Basic request specs
  2. Testing user authentication
  3. Testing user authorization
  4. Testing user input
  5. Summary
  6. Exercises

7. Next level request specs

  1. Testing invalid user input
  2. Simplifying test data with factory_bot traits
  3. Testing non-HTML requests and responses
  4. Summary
  5. Exercises

8. Testing the user interface with system specs

  1. Why system specs?
  2. System spec dependencies
  3. A basic system spec
  4. Simulating browser interactions with Capybara
  5. Debugging system specs
  6. Testing JavaScript interactions
  7. System testing strategies
  8. Summary
  9. Exercises

9. More DRY testing techniques

  1. Support modules
  2. Lazy-loading with let
  3. Shared contexts
  4. Custom matchers
  5. Aggregating failures
  6. Summary
  7. Exercise

10. Testing in isolation

  1. Mocks, stubs, doubles, fakes, and spies, defined
  2. Testing edge cases with mocks
  3. Simulating external systems
  4. Recording HTTP transactions with VCR
  5. Mocking methods in factories
  6. When mocking goes too far
  7. Summary
  8. Exercises

11. Testing the rest

  1. Testing file uploads
  2. Testing email delivery
  3. Testing background jobs
  4. Measuring test coverage
  5. Summary
  6. Exercises

12. Toward test-driven development

  1. Requirements
  2. Top-down testing
  3. Digging for clues
  4. Dropping a level
  5. Summary
  6. Excercises

13. Parting advice

  1. Practice testing the small things
  2. Be aware of what you’re doing
  3. Short spikes are OK
  4. Write a little, test a little is also OK
  5. Try to write integration specs first
  6. Make time for testing
  7. Keep it simple
  8. Don’t revert to old habits!
  9. Use your tests to make your code better
  10. Sell others on the benefits of automated testing
  11. Keep practicing
  12. Goodbye, for now

About Everyday Rails

About the author

Colophon

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub