0
0
Swiftprogramming~10 mins

Test doubles (mocks, stubs) in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Test doubles (mocks, stubs)
Start Test
Create Stub
Stub returns fixed data
Create Mock
Mock records calls
Run Code Under Test
Verify Mock Calls
Assert Results
Test Ends
The test creates stubs to provide fixed data and mocks to record calls, runs the code, then verifies interactions and results.
Execution Sample
Swift
protocol Service {
  func fetchData() -> String
}

class StubService: Service {
  func fetchData() -> String { "stub data" }
}

class MockService: Service {
  var called = false
  func fetchData() -> String {
    called = true
    return "mock data"
  }
}
Defines a service protocol, a stub returning fixed data, and a mock recording if fetchData() was called.
Execution Table
StepActionStub fetchData() returnsMock fetchData() called?Mock fetchData() returns
1Create StubService instanceN/AfalseN/A
2Call stub.fetchData()"stub data"falseN/A
3Create MockService instanceN/AfalseN/A
4Call mock.fetchData()N/Atrue"mock data"
5Verify mock.calledN/AtrueN/A
6Assert stub returns expected data"stub data"trueN/A
7Test endsN/AtrueN/A
💡 Test ends after verifying mock was called and stub returned expected data.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
stub.fetchData() returnN/A"stub data""stub data""stub data"
mock.calledfalsefalsetruetrue
mock.fetchData() returnN/AN/A"mock data""mock data"
Key Moments - 3 Insights
Why does the stub return fixed data without changing any state?
Because the stub's fetchData() method always returns the same fixed string, as shown in execution_table step 2, it does not record calls or change variables.
How do we know the mock's fetchData() method was called?
The mock sets its 'called' variable to true when fetchData() runs, shown in execution_table step 4 and variable_tracker mock.called changes from false to true.
Why do we use both a stub and a mock in testing?
The stub provides controlled data for the test, while the mock lets us verify interactions happened, as seen in the flow and execution_table steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of mock.called after calling fetchData()?
Afalse
Btrue
Cnil
D"mock data"
💡 Hint
Check the 'Mock fetchData() called?' column at step 4 in execution_table.
At which step does stub.fetchData() return its fixed data?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Stub fetchData() returns' column in execution_table.
If mock.fetchData() did not set called = true, what would be the value of mock.called at step 5?
Afalse
Bnil
Ctrue
D"mock data"
💡 Hint
Refer to variable_tracker for mock.called changes after step 4.
Concept Snapshot
Test doubles help isolate code during testing.
Stubs provide fixed data without side effects.
Mocks record calls to verify interactions.
Use stubs to control inputs, mocks to check behavior.
Verify mocks after running code under test.
Full Transcript
This visual execution shows how test doubles work in Swift. First, a stub is created that returns fixed data when fetchData() is called. Then a mock is created that records if fetchData() was called by setting a boolean variable. The code under test calls these methods. We track the stub returning fixed data and the mock recording the call. Finally, the test verifies the mock was called and the stub returned expected data. This helps isolate and check code behavior during testing.