A test double is a simple object that stands in for a real object during testing. It helps isolate the part of the code being tested by mimicking the behavior of real components.
Click to reveal answer
intermediate
What is the difference between a mock and a stub?
A stub provides predefined responses to calls made during the test, while a mock also records how it was used, allowing verification of interactions.
Click to reveal answer
beginner
Why use mocks in Swift unit tests?
Mocks help check if certain methods were called and with what data, making sure the tested code interacts correctly with other parts.
Click to reveal answer
intermediate
How does a stub help in testing asynchronous code?
A stub can return fixed data immediately or after a delay, simulating asynchronous responses without real network calls.
Click to reveal answer
beginner
Give an example of when to use a stub instead of a mock.
Use a stub when you only need to provide fixed data for a test, like returning a canned response from a database, without checking how it was called.
Click to reveal answer
Which test double records how it was used during a test?
AMock
BStub
CFake
DSpy
✗ Incorrect
Mocks record interactions so you can verify if methods were called as expected.
What is the main purpose of a stub in testing?
ATo verify method calls
BTo generate random data
CTo replace the entire system
DTo provide canned responses
✗ Incorrect
Stubs provide fixed responses to help tests run without real dependencies.
In Swift, which test double would you use to check if a function was called with specific arguments?
AStub
BMock
CDummy
DReal object
✗ Incorrect
Mocks allow verification of method calls and arguments.
Which of these is NOT a typical use of test doubles?
AIsolate the code under test
BSpeed up tests by avoiding real dependencies
CReplace user interface elements
DControl test data responses
✗ Incorrect
Test doubles replace code dependencies, not UI elements.
What is a key benefit of using test doubles in unit testing?
AThey help test code in isolation
BThey make tests slower
CThey increase code complexity
DThey replace production code permanently
✗ Incorrect
Test doubles isolate the code under test by replacing dependencies.
Explain the roles of mocks and stubs in unit testing and when to use each.
Think about whether you need to verify interactions or just provide data.
You got /4 concepts.
Describe how test doubles help improve the quality and speed of Swift unit tests.
Consider what happens if you test with real network or database calls.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a mock in Swift testing?
easy
A. To record how a function was called during a test
B. To provide fixed data without recording calls
C. To replace the entire app with a test version
D. To speed up the app in production
Solution
Step 1: Understand what a mock does
A mock is a test double that records how it was used, such as which methods were called and with what arguments.
Step 2: Compare with other test doubles
Unlike stubs, mocks focus on interaction verification, not just providing fixed answers.
Final Answer:
To record how a function was called during a test -> Option A
Quick Check:
Mock = Records calls [OK]
Hint: Mocks remember calls; stubs just return data [OK]
Common Mistakes:
Confusing mocks with stubs
Thinking mocks speed up production
Believing mocks replace the whole app
2. Which of the following is the correct way to define a stub method in Swift that always returns 42?
easy
A. func getNumber() -> Int { return 42 }
B. func getNumber() -> Int { print(42) }
C. func getNumber() { return 42 }
D. func getNumber() -> String { return "42" }
Solution
Step 1: Check method signature and return type
The method must return an Int, so the signature should be func getNumber() -> Int.
Step 2: Verify the return statement
The method should return the integer 42, so return 42 is correct.
Final Answer:
func getNumber() -> Int { return 42 } -> Option A
Quick Check:
Stub returns fixed value [OK]
Hint: Stub methods return fixed values with correct return type [OK]
Common Mistakes:
Missing return type in function signature
Returning wrong data type
Using print instead of return
3. Consider this Swift mock class snippet:
class ServiceMock {
var fetchCalled = false
func fetchData() {
fetchCalled = true
}
}
let mock = ServiceMock()
mock.fetchData()
print(mock.fetchCalled)
What will be printed?
medium
A. false
B. true
C. nil
D. Compilation error
Solution
Step 1: Understand the mock class behavior
The class has a boolean property fetchCalled initially false. The method fetchData() sets it to true.
Step 2: Trace the code execution
After creating mock, calling mock.fetchData() sets fetchCalled to true. Printing it outputs true.
Final Answer:
true -> Option B
Quick Check:
Mock records call = true [OK]
Hint: Mock flags change after method call [OK]
Common Mistakes:
Assuming fetchCalled stays false
Expecting nil instead of Bool
Thinking code won't compile
4. This Swift stub class is intended to return a fixed string but has a bug:
D. Missing return statement; add return "Fixed data"
Solution
Step 1: Identify the method signature and body
The method declares it returns a String but only calls print without returning anything.
Step 2: Fix the missing return
Replace print("Fixed data") with return "Fixed data" to satisfy the return type.
Final Answer:
Missing return statement; add return "Fixed data" -> Option D
Quick Check:
Stub must return value, not print [OK]
Hint: Return values, don't print in stubs [OK]
Common Mistakes:
Confusing print with return
Changing method name unnecessarily
Changing return type incorrectly
5. You want to test a Swift class Downloader that calls a network service. To avoid real network calls, you create a mock service that records if download() was called and a stub that returns fixed data. Which approach correctly combines mocks and stubs for this test?
hard
A. Use only a mock that returns fixed data without recording calls
B. Use only a stub that records calls and returns data
C. Use a mock to record download() calls and a stub to return fixed data when fetchData() is called
D. Use real network service to ensure accurate testing
Solution
Step 1: Understand roles of mocks and stubs
Mocks record interactions like method calls; stubs provide fixed return data.
Step 2: Apply correct combination
Use a mock to check if download() was called, and a stub to supply fixed data for fetchData() to avoid real network calls.
Final Answer:
Use a mock to record download() calls and a stub to return fixed data when fetchData() is called -> Option C
Quick Check:
Mock records calls + Stub returns data [OK]
Hint: Mocks track calls; stubs provide data [OK]
Common Mistakes:
Using only one test double type
Relying on real network in tests
Confusing mock and stub roles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.