uber / mockolo

Efficient Mock Generator for Swift
Apache License 2.0
813 stars 86 forks source link

Issue when Generating Mocks with Protocol Composition #170

Closed tinder-richardchildress closed 1 year ago

tinder-richardchildress commented 2 years ago

Howdy! I am trying to generate a mock for a protocol that inherits from other protocols using protocol composition. When testing this, I tried the following:

/// @mockable
protocol FirstSwiftProtocol {
    var someBool: Bool { get }

    func doSomething()
}

/// @mockable
protocol SecondSwiftProtocol {
    var someInt: Int { get set }

    func doSomethingElse() -> Bool
}

/// @mockable
protocol TestProtocol: FirstSwiftProtocol & SecondSwiftProtocol {}

And the result of generating mocks was the following:

class TestProtocolMock: TestProtocol {
    init() { }
    init(someBool: Bool = false) {
        self.someBool = someBool
    }

    private(set) var someBoolSetCallCount = 0
    var someBool: Bool = false { didSet { someBoolSetCallCount += 1 } }

    private(set) var doSomethingCallCount = 0
    var doSomethingHandler: (() -> ())?
    func doSomething()  {
        doSomethingCallCount += 1
        if let doSomethingHandler = doSomethingHandler {
            doSomethingHandler()
        }

    }
}

which fails to compile as TestProtocolMock does not conform to SecondSwiftProtocol. Interestingly, if I wrap the conformance:

protocol TestProtocol: (FirstSwiftProtocol & SecondSwiftProtocol) {}

The mock is:

class TestProtocolMock: TestProtocol {
    init() { }

}

Additionally, if I use a typealias, I get the same empty init mock as when I wrap the conformance.

Looking for any insight as to why this may be happening. Thank you!

ankithTao commented 2 years ago

@tinder-richardchildress

check your mockolo script in your build phases, make sure that ur protocol B is defined inside the source directory of the script. ./mockolo -s ./protocolB.swift -d ./OutputMocks.swift stackoverflow

mithatsk commented 2 years ago

If your protocol extends to another protocol that was defined in a different module/target. You can include the generated mock/mocks after the -mock flag (for multiple files put a space between them)

./mockolo -s YourSourcePath -mocks OtherModulePath/TestProtocolMocks.swift  -d OutputPath/Mocks.swift