Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
133: Testing Background Jobs
When you first start moving logic into background jobs—maybe using Sidekiq or ActiveJob—there is a very strong temptation to keep testing those jobs exactly how you tested the synchronous code they replaced. You’ve got a user signing up, and you want to make sure they get a welcome email. It feels natural to write a test that creates a user and then immediately asserts that an email was delivered.
The trap of testing the side effect
The naive approach looks like this: you trigger the action that kicks off the job, and then you check if the final result happened. If you're using ActiveJob, you might even call perform_now in your test environment to force the job to run immediately so you can see the result.
it "sends a welcome email when a user is created" do
user = User.create!(email: "dev@example.com")
# Naive: checking the final side effect in the trigger test
expect(ActionMailer::Base.deliveries.last.to).to include("dev@example.com")
end
I've seen this in countless codebases, and while it feels "thorough" because you're testing the whole chain, it's actually a liability. First, it makes your tests slow. As your app grows, you'll have dozens of jobs triggered by a single request. If every single one of those jobs actually executes during your controller or service tests, your test suite will crawl.
More importantly, you're violating the principle of single responsibility in your tests. If the WelcomeEmailJob fails because of a typo in the mailer template, your User creation test fails. You now have a failing test telling you that user creation is broken, when in reality, the user was created perfectly fine—it was the background worker that choked. That's a frustrating way to debug.
Decoupling the trigger from the execution
The better way is to split your testing into two distinct concerns: did I schedule the work, and does the work itself actually work?
In your trigger test (the one creating the user), you only care that the job was put on the queue with the right arguments. You don't care what the job does; that's the job's responsibility. In Rails, we use have_enqueued_job for this. It's fast because it doesn't actually run the job logic; it just checks the queue metadata.
it "enqueues a welcome email job" do
expect {
User.create!(email: "dev@example.com")
}.to have_enqueued_job(WelcomeEmailJob).with("dev@example.com")
end
Then, you write a separate, isolated test for the WelcomeEmailJob itself. In this spec, you aren't creating a user through the API or a service; you're just calling perform on the job class and asserting that the email goes out. This is where you test the edge cases—like what happens if the user deleted their account in the three seconds between the job being enqueued and being executed.
Where the trade-off actually lands
Some developers argue that this approach "misses" the integration. They worry that you could have a passing trigger test and a passing job test, but the job still fails in production because the arguments passed don't match what the job expects.
I'll be honest: that is a theoretical risk. But the trade-off is worth it. By decoupling, you get a test suite that runs in seconds instead of minutes, and you get pinpoint accuracy when something breaks. If the job spec fails, you know exactly where the bug is. If the trigger spec fails, you know the orchestration is broken. I'll take that clarity over a "full chain" test any day of the week.
📋 Practical Task
Implement and Test a NewsletterSubscriptionJob
You are building a newsletter subscription feature. When a user submits their email via a NewsletterSubscriptionService, a NewsletterSubscriptionJob should be enqueued to handle the external API call to the mailing list provider.
Your task:
- Create a
NewsletterSubscriptionJobthat takes anemailstring and logs "Subscribing #{email} to newsletter" (to simulate the API call). - Create a
NewsletterSubscriptionServicewith acall(email)method that enqueues this job. - Write two separate tests:
- A test for the
NewsletterSubscriptionServicethat asserts theNewsletterSubscriptionJobwas enqueued with the correct email, without executing the job. - A test for the
NewsletterSubscriptionJobthat callsperformdirectly and verifies the "Subscribing..." message is logged or the expected logic is executed.
- A test for the
There are no comments for now.