Complete the code to declare a fake repository class implementing the UserRepository interface.
class FakeUserRepository : UserRepository { override fun getUser(id: Int): User { return [1] } }
The fake repository returns a dummy User object with the given id and a fixed name.
Complete the test function to verify the fake repository returns the expected user name.
fun testGetUserReturnsFake() {
val repo = FakeUserRepository()
val user = repo.getUser(1)
assert(user.name == [1])
}The fake repository returns a user with the name "Fake User" as defined in the fake.
Fix the error in the fake repository to store users in a mutable map and return them by id.
class FakeUserRepository : UserRepository { private val users = mutableMapOf<Int, User>() fun addUser(user: User) { users[[1]] = user } override fun getUser(id: Int): User? { return users[[2]] } }
The addUser function uses user.id as the key to store the user. The getUser function uses the id parameter to retrieve the user.
Fill both blanks to complete the test that adds a user to the fake repository and verifies retrieval.
fun testAddAndGetUser() {
val repo = FakeUserRepository()
val user = User(10, "Test User")
repo.[1](user)
val retrieved = repo.[2](10)
assert(retrieved?.name == "Test User")
}The test calls addUser to add the user, then getUser to retrieve it by id.
Fill all three blanks to create a fake repository with a mutable list and implement add, get, and clear methods.
class FakeUserRepository : UserRepository { private val users = mutableListOf<User>() fun [1](user: User) { users.[2](user) } fun [3]() { users.clear() } override fun getUser(id: Int): User? { return users.find { it.id == id } } }
The method to add a user is named addUser, which calls the list's add method. The method to clear users is named clear, which calls users.clear().