0
0
Rubyprogramming~10 mins

Test doubles concept in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Test doubles concept
Start Test
Create Test Double
Define Expected Calls
Run Code Using Double
Verify Calls
Pass or Fail Test
End
The test creates a double, sets expected calls, runs code using it, then verifies if calls match expectations.
Execution Sample
Ruby
double = double('User')
allow(double).to receive(:name).and_return('Alice')
puts double.name
expect(double).to have_received(:name)
This code creates a test double for 'User', sets it to return 'Alice' when name is called, prints the name, and checks if name was called.
Execution Table
StepActionEvaluationResult
1Create double 'User'double createddouble object ready
2Stub method :name to return 'Alice'method :name stubbeddouble.name returns 'Alice'
3Call double.namedouble.name called'Alice' returned and printed
4Verify double received :name callcheck call historycall found, verification passes
5Test endsall expectations mettest passes
💡 Test ends after verifying all expected calls were made successfully.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
doublenildouble objectdouble with :name stubdouble with :name calleddouble with call verified
Key Moments - 3 Insights
Why do we create a test double instead of using the real object?
We create a test double to isolate the test from real object behavior and dependencies, as shown in step 1 of the execution_table.
How does the test know if the double's method was called?
The test tracks method calls on the double internally, verified in step 4 where it checks the call history.
What happens if the expected method is not called on the double?
The verification in step 4 would fail, causing the test to fail because the expected call was missing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 3 when double.name is called?
AMethod :name stubbed
B'Alice' returned and printed
CCall found, verification passes
DDouble object ready
💡 Hint
Check the 'Result' column in row for step 3 in execution_table.
At which step does the test verify if the double received the expected method call?
AStep 2
BStep 1
CStep 4
DStep 3
💡 Hint
Look at the 'Action' column in execution_table to find when verification happens.
If we did not stub the method :name to return 'Alice', what would happen at step 3?
AThe call would return nil or error
B'Alice' would still be returned
CVerification would pass automatically
DDouble would not be created
💡 Hint
Refer to step 2 and 3 in execution_table about stubbing and method call results.
Concept Snapshot
Test doubles are fake objects used in tests to stand in for real ones.
Create a double, stub methods to return expected values.
Run code using the double, then verify expected calls happened.
This isolates tests and controls behavior for reliable testing.
Full Transcript
This visual execution shows how test doubles work in Ruby. First, a double object is created to represent a real object. Then, we stub a method on the double to return a fixed value. When the method is called, it returns the stubbed value. The test then verifies that the method was called on the double. If all expected calls happen, the test passes. This helps test code in isolation without relying on real objects.