Mock Firebase Auth in Swift

Viewed 27

I use Firebase with Swift.

I wanted to write tests for it, so I tried to mock various methods of FirebaseAuth.

I referred to the following post, but I could not use it because I could not override the init of MockUser, which inherits from Firebase.User.

Mocking iOS Firebase Auth sign-in methods

So I would like to ask how to make Firebase Auth's SignIn(withEmail:, password: , completion:) etc. Mock.

Thank you in advance.

Here is the code I wrote

protocol FirebaseAuthDataResultType {
    var user: Firebase.User { get }
}

extension AuthDataResult: FirebaseAuthDataResultType {}

typealias FirebaseAuthDataResultTypeCallback = (FirebaseAuthDataResultType?, Error?) -> Void

protocol AuthRepositoryInput {
    func signIn(email: String, password: String, handler: FirebaseAuthDataResultTypeCallback?)
    func signOut (completion: @escaping (Bool) -> ())
    func addListeners(completion: @escaping (User?, Bool) -> Void)
    func deleteListeners()
    func sendEmailVerification(completion: @escaping (Error?) -> Void)
}

class MockUser: FirebaseAuth.User { // ← cannot override init
    let testingUID: String
    let testingEmail: String?
    let testingDisplayName: String?
}

struct MockFirebaseAuthDataResult: FirebaseAuthDataResultType {
    var user: Firebase.User
}

final class AuthRepositoryMock: AuthRepositoryInput {
    typealias AuthDataResultType = (authDataResult: FirebaseAuthDataResultType?, error: Error?)
    
    var authDataResultFactory: (() -> (AuthDataResultType))?
    
    
    func signIn(email: String, password: String, handler: FirebaseAuthDataResultTypeCallback?) {
...snip...
0 Answers
Related