简体   繁体   中英

How to override init(coder: NSCoder) for FacebookAuthToken

I'm trying to create a mock subclass for facebook AuthenticationToken This object has only one open init function

init(coder: NSCoder) { }

But I'm getting an error while I'm trying to create my init and call super.init of this class

init?(_ stringToken: String) {
    stubString = stringToken
    super.init(coder: NSCoder())
}

With the next error在此处输入图像描述

The question is: How could I create an instance of this AuthenticationToken ?

You can't initiate this class from outside. What I recommend is creating a protocol for the AuthenticationToken which you can mock. The Protocol should hold all functions you're using from the AuthenticationToken.

example:

protocol AuthenticationTokenProtocol {
    func a() // function a which you might use
    func b() // function b which you might use
}

extension AuthenticationToken: AuthenticationTokenProtocol {}

final class AuthenticationTokenMock: AuthenticationTokenProtocol {
    func a() { // mock it 
    }
    func b() { // mock it 
    }
} 

You can then inject always the AuthenticationTokenProtocol instead of the real AuthenticationToken.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM