0
0
Rubyprogramming~10 mins

Mocking and stubbing in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mocking and stubbing
Start Test
Create Stub
Call Method
Return Stubbed Value
Create Mock
Expect Method Call
Verify Mock
End Test
The test starts by creating stubs to replace methods with fixed returns, then mocks to expect calls, and finally verifies those expectations before ending.
Execution Sample
Ruby
class User
  def name; 'Alice'; end
end

user = User.new
allow(user).to receive(:name).and_return('Bob')
puts user.name
This code replaces the User's name method to always return 'Bob' instead of 'Alice'.
Execution Table
StepActionMethod CalledStub/Mock SetupReturn ValueOutput
1Create User instance----
2Stub 'name' method on usernameStub returns 'Bob'--
3Call 'name' method on username-BobBob
4End of test----
💡 Test ends after method call returns stubbed value 'Bob'
Variable Tracker
VariableStartAfter Stub SetupAfter Method CallFinal
user.nameAliceStubbed to 'Bob'Returns 'Bob'Bob
Key Moments - 2 Insights
Why does user.name return 'Bob' instead of 'Alice'?
Because in step 2 of the execution_table, the 'name' method is stubbed to always return 'Bob', overriding the original method.
What is the difference between a stub and a mock here?
A stub replaces a method to return a fixed value (step 2), while a mock would also check if the method was called as expected (not shown in this simple example).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value of user.name at step 3?
A'Alice'
B'Bob'
Cnil
DError
💡 Hint
Check the 'Return Value' column at step 3 in the execution_table.
At which step is the stub for the 'name' method created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Stub/Mock Setup' column to find when the stub is applied.
If we remove the stub setup in step 2, what would user.name return at step 3?
A'Alice'
Bnil
C'Bob'
DError
💡 Hint
Refer to variable_tracker for user.name initial value before stubbing.
Concept Snapshot
Mocking and stubbing in Ruby:
- Stub replaces a method to return fixed value.
- Mock sets expectations on method calls.
- Use 'allow(obj).to receive(:method).and_return(value)' for stubbing.
- Use 'expect(obj).to receive(:method)' for mocking.
- Verify mocks to ensure methods were called.
- Helps isolate tests from real dependencies.
Full Transcript
This visual trace shows how mocking and stubbing work in Ruby tests. First, a User object is created with a name method returning 'Alice'. Then, the name method is stubbed to return 'Bob' instead. When calling user.name, the stubbed value 'Bob' is returned instead of the original 'Alice'. This helps tests control method outputs. Mocks would also check if methods are called as expected, but this example focuses on stubbing. The variable tracker shows how user.name changes from 'Alice' to 'Bob' after stubbing. The key moments clarify why the stub changes the return value and the difference between mocks and stubs. The quiz questions help reinforce understanding by referencing the execution steps and variable states.