Protocols in Swift define required methods and properties. Mocks implement these protocols to stand in for real objects during tests, enabling controlled and isolated testing.
Authenticator with a method login(username:password:), which mock implementation correctly simulates a successful login for testing a login screen UI?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 ???
}
}Returning true simulates a successful login, allowing the UI to proceed as if authentication succeeded.
Creating a fresh mock in setUp() ensures each test starts with a clean state, preventing tests from affecting each other.
DataFetcher with a method fetchData() -> String?protocol DataFetcher {
func fetchData() -> String
}Option A correctly implements the protocol method with matching return type. Others either have wrong return type, missing protocol conformance, or wrong method signature.
fatalError() immediately stops program execution with a crash. Using it in a mock method causes tests to crash if that method is called.
