Why RSpec Tests Fail (and How To Fix Them)

Knowing how to troubleshoot tests is an essential software skill

Thomas Barrasso
Oct 7, 2019 · 13 min read
Image for post
Image for post
NASA First Launch, 1950 🚀
Image for post
Image for post
Wan Hu, Wikipedia

Why Test Software


How RSpec Tests Fail


Forgetfulness

Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. — Martin Fowler, Mocks Aren’t Stubs

Failure/Error: expect(house.price).to be > 649000
expected: > 649000
got: 649000

In-Memory and In-Database Discrepancies

Failure/Error: expect(user).to receive(:perform_send_welcome_email!)

(#<User:#..>).perform_send_welcome_email!(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments
Failure/Error: expect(user.welcome_email_sent?).to eq(true)

expected: true
got: false

(compared using ==)

Non-Determinism

Image for post
Image for post
Random Number Generators

Randomness

Unique constraints


State Preservation Across Tests

Failure/Error: raise AlreadyVotedError

# ./voter.rb:26:in `vote'

Network Calls

# --- Caused by: ---
# Net::HTTPServerException:
# 503 "Service Unavailable"

Time


Side Effects

Active record callbacks


Active Job QueueAdapters


Too Specific


Negative Test Expectations



Final Thoughts

The bitterness of poor quality remains long after the sweetness of meeting the schedule has been forgotten.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store