0
0
iOS Swiftmobile~5 mins

Mock objects and protocols in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a protocol in Swift?
A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. Classes, structs, or enums can adopt protocols to provide actual implementations.
Click to reveal answer
beginner
Why do we use mock objects in iOS testing?
Mock objects simulate real objects in tests. They help isolate the code being tested by replacing dependencies with controllable and predictable versions, making tests faster and more reliable.
Click to reveal answer
intermediate
How do protocols help with creating mock objects?
Protocols allow you to define interfaces that real objects and mock objects both conform to. This lets you swap the real implementation with a mock one easily during testing without changing the code that uses them.
Click to reveal answer
intermediate
What is a key benefit of using protocols for dependency injection?
Protocols enable dependency injection by allowing you to pass any object that conforms to the protocol. This makes your code more flexible and testable because you can inject mock objects during tests.
Click to reveal answer
beginner
In Swift, how do you declare that a class conforms to a protocol?
You add the protocol name after the class name, separated by a colon. For example: class MyClass: MyProtocol { }
Click to reveal answer
What is the main purpose of a mock object in testing?
ATo add new features to the app
BTo speed up the app in production
CTo replace real objects with controllable versions
DTo store user data securely
Which Swift feature allows you to define a set of methods that a class must implement?
AProtocol
BEnum
CStruct
DExtension
How do mock objects improve test reliability?
ABy isolating dependencies and controlling behavior
BBy introducing random data
CBy making tests run slower
DBy connecting to real databases
What keyword do you use in Swift to declare a protocol?
Aclass
Bprotocol
Cinterface
Dstruct
Which of these is a benefit of using protocols for dependency injection?
AHardcoding dependencies
BMaking code less testable
CIncreasing app size
DAllowing easy swapping of implementations
Explain how protocols and mock objects work together to improve testing in Swift.
Think about how you can replace a real part of your app with a fake one during tests.
You got /4 concepts.
    Describe the steps to create a mock object for a protocol in Swift.
    Start with the protocol, then build a fake version that follows its rules.
    You got /4 concepts.