0
0
iOS Swiftmobile~20 mins

Mock objects and protocols in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mock Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use protocols for mocking in Swift?
In Swift, why do developers often use protocols when creating mock objects for testing?
AProtocols define a blueprint that mocks can implement, allowing tests to substitute real objects easily.
BProtocols automatically generate mock objects without extra code.
CProtocols prevent any code from being tested unless mocks are used.
DProtocols are only used to improve app performance, not for mocking.
Attempts:
2 left
💡 Hint
Think about how protocols help replace real objects with test doubles.
ui_behavior
intermediate
1:30remaining
Mock object behavior in a login test
Given a protocol Authenticator with a method login(username:password:), which mock implementation correctly simulates a successful login for testing a login screen UI?
iOS Swift
protocol Authenticator {
  func login(username: String, password: String) -> Bool
}

class MockAuthenticator: Authenticator {
  func login(username: String, password: String) -> Bool {
    // Which return value simulates success?
    return ???
  }
}
Areturn true
Breturn false
Creturn nil
Dthrow an error
Attempts:
2 left
💡 Hint
Success means the login method should indicate a positive result.
lifecycle
advanced
2:00remaining
Mock object lifecycle in unit tests
In Swift unit tests, when using a mock object that conforms to a protocol, what is the best practice to ensure the mock's state resets between tests?
ACreate a new mock instance in the <code>setUp()</code> method before each test runs.
BReuse the same mock instance for all tests to save memory.
CCreate the mock once in <code>init()</code> and never reset it.
DUse global variables to store mock state across tests.
Attempts:
2 left
💡 Hint
Think about test isolation and avoiding shared state.
📝 Syntax
advanced
2:00remaining
Correct protocol mock syntax in Swift
Which of the following Swift code snippets correctly defines a mock class that conforms to the protocol DataFetcher with a method fetchData() -> String?
iOS Swift
protocol DataFetcher {
  func fetchData() -> String
}
A
class MockFetcher: DataFetcher {
  func fetchData() -&gt; String {
    return "mock data"
  }
}
B
class MockFetcher: DataFetcher {
  func fetchData() {
    return "mock data"
  }
}
C
class MockFetcher: DataFetcher {
  func fetchData() -&gt; Int {
    return 123
  }
}
D
class MockFetcher {
  func fetchData() -&gt; String {
    return "mock data"
  }
}
Attempts:
2 left
💡 Hint
Check method signature matches protocol exactly.
🔧 Debug
expert
2:30remaining
Why does this mock cause a runtime error?
Consider this mock class in Swift: class MockService: ServiceProtocol { func fetchData() -> String { fatalError("Not implemented") } } Why will using this mock in tests cause a runtime crash?
ABecause calling fetchData() triggers fatalError, which stops the program immediately.
BBecause the mock does not conform to ServiceProtocol correctly.
CBecause fetchData() returns the wrong data type.
DBecause fatalError is only allowed in production code, not tests.
Attempts:
2 left
💡 Hint
What does fatalError() do when called?