InstantMock icon indicating copy to clipboard operation
InstantMock copied to clipboard

Problem implementing MockUsable when there are inheritances

Open igordeoliveirasa opened this issue 8 years ago • 6 comments

Do you have any solution for the case below? AuthenticationSession extends Model. However, I need to make both of then implement MockUsable. Since you are using static var, its impossible to override..

extension AuthenticationSession_: MockUsable {
    public static var any: AuthenticationSession_ {
        let ret = AuthenticationSession_()
        ret.id = "anyId"
        return ret
    }
    
    public static var anyValue: MockUsable {
        return self.any
    }
    
    public func equal(to value: MockUsable?) -> Bool {
        guard let mock = value as? AuthenticationSession_ else { return false }
        return self.id == mock.id
    }
}

extension Model: MockUsable {
    public static var any: Model {
        let ret = Model()
        return ret
    }
    
    public static var anyValue: MockUsable {
        return self.any
    }
    
    public func equal(to value: MockUsable?) -> Bool {
        guard let mock = value as? Model else { return false }
        return false
    }
}

igordeoliveirasa avatar Oct 20 '17 14:10 igordeoliveirasa

screen shot 2017-10-20 at 12 19 19 1

igordeoliveirasa avatar Oct 20 '17 14:10 igordeoliveirasa

Yes for now, we can't make a class MockUsable, if it inherits from another MockUsable class.

We should avoid static variables indeed, but the challenge is to make sure the MockUsable protocol can return an instance of the given type. Don't hesitate to share ideas on how we could deal with this.

pirishd avatar Oct 22 '17 17:10 pirishd

Best option I have in mind would be to make usage of factories for MockUsable types, like:

protocol MockUsableFactory {
    associatedtype T: MockUsable
    func anyValue() -> T
}

Those factories would have to be provided by Mock instances through a MockUsableFactoryProvider protocol like:

protocol MockUsableFactoryProvider {
    var factories: [MockUsableFactory] { get }
}

That would make the creation of mocks a bit more complex.

pirishd avatar Oct 24 '17 06:10 pirishd

I'll thing about it...

igordeoliveirasa avatar Oct 25 '17 11:10 igordeoliveirasa

I don't have any idea. However, It would not be nice to complicate the current way.

I'm really getting stuck since I can't for example have a UIViewController as a MockUsable and a HomeViewController as a MockUsable, knowing that HomeViewController extends UIViewController, as all of the other views.

I really would appreciate if we could have a new version solving this issue.

igordeoliveirasa avatar Oct 28 '17 22:10 igordeoliveirasa

@pirishd any news?

igordeoliveirasa avatar Nov 17 '17 16:11 igordeoliveirasa