0
0
Swiftprogramming~5 mins

Test doubles (mocks, stubs) in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a test double in software testing?
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
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
In Swift, which test double would you use to check if a function was called with specific arguments?
AStub
BMock
CDummy
DReal object
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
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
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.