0
0
Swiftprogramming~10 mins

Test doubles (mocks, stubs) in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a stub class that returns a fixed value.

Swift
class UserServiceStub: UserServiceProtocol {
    func fetchUser() -> User {
        return [1]
    }
}
Drag options to blanks, or click blank then click option'
AUser(name: "TestUser")
BfetchUser()
CUserService()
DUser()
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a method call instead of a User object.
Returning an empty User without parameters.
2fill in blank
medium

Complete the code to verify the mock method was called once.

Swift
class UserServiceMock: UserServiceProtocol {
    var fetchUserCalled = false
    func fetchUser() -> User {
        fetchUserCalled = true
        return User(name: "MockUser")
    }
}

let mock = UserServiceMock()
_ = mock.fetchUser()
assert([1] == true)
Drag options to blanks, or click blank then click option'
AfetchUserCalled
Bmock.fetchUser()
Cmock.fetchUserCalled
DUserServiceMock.fetchUserCalled
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the method again inside assert.
Using class name instead of instance.
3fill in blank
hard

Fix the error in the mock class to count how many times fetchUser is called.

Swift
class UserServiceMock: UserServiceProtocol {
    var fetchUserCallCount = 0
    func fetchUser() -> User {
        [1]
        return User(name: "MockUser")
    }
}
Drag options to blanks, or click blank then click option'
AfetchUserCallCount--
BfetchUserCallCount = 1
CfetchUserCallCount =+ 1
DfetchUserCallCount += 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using = 1 resets count every call.
Using -- decreases count incorrectly.
Using =+ 1 is a syntax error.
4fill in blank
hard

Fill both blanks to create a stub that returns a user with a dynamic name and verify the name.

Swift
class UserServiceStub: UserServiceProtocol {
    var userName: String
    init(name: String) {
        self.userName = [1]
    }
    func fetchUser() -> User {
        return User(name: [2])
    }
}

let stub = UserServiceStub(name: "Alice")
let user = stub.fetchUser()
assert(user.name == "Alice")
Drag options to blanks, or click blank then click option'
Aname
B"Alice"
CuserName
Dself.userName
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed string instead of the parameter.
Returning the parameter directly instead of the property.
5fill in blank
hard

Fill all three blanks to create a mock that records calls and returns a stubbed user.

Swift
class UserServiceMock: UserServiceProtocol {
    var fetchUserCallCount = 0
    var stubUser: User
    init(stubUser: User) {
        [1] = stubUser
    }
    func fetchUser() -> User {
        [2]
        return [3]
    }
}

let stub = User(name: "Bob")
let mock = UserServiceMock(stubUser: stub)
_ = mock.fetchUser()
assert(mock.fetchUserCallCount == 1)
assert(mock.stubUser.name == "Bob")
Drag options to blanks, or click blank then click option'
Aself.stubUser
BfetchUserCallCount += 1
CstubUser
Dself.fetchUserCallCount
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning stubUser without self.
Returning stubUser without self.
Not incrementing call count.