0
0
Swiftprogramming~20 mins

Test doubles (mocks, stubs) in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Test Doubles Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a stubbed method call
What is the output of this Swift code using a stub to replace a method?
Swift
protocol DataService {
    func fetchData() -> String
}

class StubDataService: DataService {
    func fetchData() -> String {
        return "Stubbed Data"
    }
}

func printData(service: DataService) {
    print(service.fetchData())
}

let stub = StubDataService()
printData(service: stub)
AStubbed Data
BfetchData()
CDataService
DRuntime error
Attempts:
2 left
💡 Hint
The stub replaces the real method with a fixed return value.
Predict Output
intermediate
2:00remaining
Mock method call count
What will be printed after running this Swift code that uses a mock to count method calls?
Swift
class MockLogger {
    var logCount = 0
    func log(_ message: String) {
        logCount += 1
        print("Log: \(message)")
    }
}

let mock = MockLogger()
mock.log("Start")
mock.log("Process")
print(mock.logCount)
A2
B0
CCompile error
D1
Attempts:
2 left
💡 Hint
The mock increments logCount every time log() is called.
🔧 Debug
advanced
2:00remaining
Why does this mock not record calls?
This Swift mock class is supposed to record calls to the method but fails. What is the cause?
Swift
class MockService {
    var callCount = 0
    func performAction() {
        var callCount = 0
        callCount += 1
    }
}

let mock = MockService()
mock.performAction()
mock.performAction()
print(mock.callCount)
AcallCount is a constant and cannot be changed.
BThe performAction method is never called.
CThe callCount inside performAction is a new local variable, not the class property.
Dprint statement is outside the class and cannot access callCount.
Attempts:
2 left
💡 Hint
Check variable scope inside the method.
🧠 Conceptual
advanced
2:00remaining
Purpose of a stub in testing
Which statement best describes the purpose of a stub in unit testing?
AA stub replaces the entire application logic with a fake implementation for integration testing.
BA stub provides canned responses to calls made during the test, isolating the tested code from external dependencies.
CA stub records every call made to it and verifies the order of calls.
DA stub automatically generates test data based on input parameters.
Attempts:
2 left
💡 Hint
Think about how stubs help isolate code under test.
Predict Output
expert
2:00remaining
Output of a mock verifying call sequence
What is the output of this Swift code that uses a mock to verify the order of method calls?
Swift
class MockSequence {
    var calls: [String] = []
    func first() { calls.append("first") }
    func second() { calls.append("second") }
    func verify() -> Bool {
        return calls == ["first", "second"]
    }
}

let mock = MockSequence()
mock.second()
mock.first()
print(mock.verify())
ARuntime error
Btrue
CCompile error
Dfalse
Attempts:
2 left
💡 Hint
Check the order in which methods are called and recorded.