
February 10, 2025
Testing in RSpec is like running a clean, well-organized kitchen. Every test should start fresh, without yesterday’s spaghetti sauce all over the counter. But if you’ve ever run into mysterious test failures, you’ve likely witnessed the horrors of a “dirty environment.”
Fear not, fellow Rubyist! Today, we embark on an epic journey through the mystical RSpec blocks—before, after, around—and reveal the ultimate method to clean your test environment.
Need Expert Ruby on Rails Developers to Elevate Your Project?

Chapter 1: The before Block – Setting the Stage
The before block in RSpec is like a diligent stagehand, making sure everything is in place before the show begins.
RSpec.describe "Magical Spec" do
before(:each) do
@wizard = "Gandalf"
end
it "summons the wizard" do
expect(@wizard).to eq("Gandalf")
end
end
There are two main types:
- before(:each) → Runs before each test.
- before(:all) → Runs once before all tests in the group (use with caution!).
But what happens when Gandalf overstays his welcome? That’s where after comes in…
Chapter 2: The after Block – Cleaning Up the Mess
After a test, we might need to clean up—just like washing dishes after cooking.
RSpec.describe "Messy Spec" do
after(:each) do
@wizard = nil
end
it "summons the wizard" do
@wizard = "Saruman"
end
end
Types of after:
- after(:each) → Runs after each test.
- after(:all) → Runs once after all tests in the group.
Chapter 3: The around Block – The Zen Master of RSpec
Imagine a monk who surrounds your test with wisdom—before and after meditation.
RSpec.describe "Zen Spec" do
around(:each) do |example|
puts "Clearing the mind..."
example.run
puts "Returning to serenity."
end
it "embraces the moment" do
puts "Mindfulness achieved!"
end
end
This outputs:
Clearing the mind... Mindfulness achieved! Returning to serenity.
Perfect for ensuring setup and teardown in one place.
Chapter 4: The Grand Ritual – Cleaning the Test Environment
Now, what if your tests keep failing because previous tests left a mess? Time to exorcise the ghosts of tests past!
1. The RSpec Way: Reset Global State
RSpec.configure do |config|
config.before(:each) do
ENV["SECRET_CODE"] = nil
end
end
This ensures no test inherits a cursed environment.
2. Database Exorcism: The DatabaseCleaner Ritual

If you’re using ActiveRecord, the DatabaseCleaner gem is your best friend.
RSpec.configure do |config|
require 'database_cleaner-active_record'
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
No more leftover database records haunting your tests!
3. Class Variables & Singletons: The Reset Button
RSpec.configure do |config|
config.before(:each) do
MySingleton.instance_variable_set(:@cache, nil)
end
end
Even singletons get a fresh start!
4. The Ultimate Reset: Reloading Rails
Extreme, but effective for persistent app state.
RSpec.configure do |config|
config.before(:each) do
Rails.application.reloader.reload!
end
end
Use sparingly—this slows tests down.
Final Words: Keep It Clean!
RSpec blocks help you prepare, execute, and clean up each test. By properly resetting your environment, you’ll avoid flaky tests, random failures, and debugging nightmares.
Now go forth, write clean, isolated tests, and may your specs always pass! 🚀
