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
102: RSpec Matchers In Depth
You've likely already spent some time using expect().to eq(). It's the workhorse of RSpec, and for a lot of basic tests, it's all you need. But as you start building more complex systems, you'll notice a pattern: you're writing three or four lines of tests to verify one single conceptual change. I see this all the time in code reviews. We're testing the implementation of the data structure rather than the intent of the behavior.
The trap of the generic equality check
Imagine we're building a simple ShoppingCart. We want to make sure that when we add a product, the cart actually holds that product and the count goes up. A naive approach—the kind I used to write early in my career—looks something like this:
it "adds an item to the cart" do
cart = ShoppingCart.new
item = Product.new(name: "Mechanical Keyboard")
cart.add(item)
expect(cart.items.count).to eq(1)
expect(cart.items.first).to eq(item)
end
On the surface, this works. It's green. But it's brittle. I'm explicitly checking the count and then peering into the array to see if the first element is the item. If I later change the ShoppingCart to store items in a Hash or a Set for performance reasons, this test breaks—not because the feature is broken, but because the test was too interested in the internal array structure.
Capturing intent with specialized matchers
The goal is to write tests that describe what should happen, not how the data is stored. RSpec gives us specialized matchers that do the heavy lifting for us. Instead of checking the count and the first element separately, we can use include and the change matcher.
it "adds an item to the cart" do
cart = ShoppingCart.new
item = Product.new(name: "Mechanical Keyboard")
expect { cart.add(item) }.to change { cart.items.count }.by(1)
expect(cart.items).to include(item)
end
This is a subtle shift, but it's a powerful one. The change matcher allows us to wrap the action (adding the item) and the expectation (the count increasing) into a single atomic operation. It reads like a sentence: "I expect that adding an item to change the items count by one."
When 'eq' tells you nothing about why it failed
The biggest trade-off here isn't actually about the code's cleanliness—it's about the failure messages. When expect(cart.items.first).to eq(item) fails, RSpec tells you that nil does not equal #<Product...>. That's fine, but it doesn't tell you why.
If you use match_array, for instance, RSpec does something clever. If you expected [:apple, :orange] but got [:apple, :banana], RSpec won't just say "they aren't equal." It will explicitly tell you that :orange was expected but missing, and :banana was an unexpected extra. I've spent hours debugging tests that used eq on large arrays, squinting at a wall of text to find one missing element. Using a matcher like match_array or include turns a ten-minute hunt into a five-second fix.
Ultimately, the rule of thumb I follow is this: if you find yourself calling .count, .first, or .any? inside an expect block, you're probably using the wrong matcher. Look for one that describes the relationship between the objects rather than the state of the variable.
📋 Practical Task
Refactoring the UserRegistration Service Tests
You've inherited a test suite for a UserRegistration service. The current tests are "brittle"—they rely on checking array indexes and boolean flags instead of using RSpec's more expressive matchers. Your goal is to refactor the test file to use specialized matchers.
The Scenario: The UserRegistration service takes a user's email and a list of "interest tags" (e.g., ['ruby', 'testing', 'architecture']). When a user registers, the service should:
1. Increase the total registered user count by 1.
2. Ensure the user's tags list contains all the provided interests, regardless of the order they were saved in.
The Brittle Code:
# current_spec.rb
it "registers a user with tags" do
service = UserRegistration.new
tags = ['ruby', 'testing']
initial_count = User.count
service.register("dev@example.com", tags)
expect(User.count).to eq(initial_count + 1)
user = User.last
expect(user.tags.count).to eq(2)
expect(user.tags[0]).to eq('ruby')
expect(user.tags[1]).to eq('testing')
end
Your Task: Rewrite the test block above. Replace the manual count tracking with a change matcher and replace the index-based tag checks with a single match_array matcher. This will ensure the test remains green even if the database returns the tags in a different order.
There are no comments for now.