Kolos65 / Mockable

A Swift macro driven auto-mocking library.
MIT License
199 stars 14 forks source link

Protocol inheritance and composition #24

Closed VisualDeceit closed 3 months ago

VisualDeceit commented 3 months ago

Hello. Is there a way to mocking protocols that combine multiple protocols? I try some like this, but got the error.

protocol ServiceA {
    func fooA()
}

protocol ServiceB {
    func fooB()
}

@Mockable
protocol SummaryService: ServiceA, ServiceB {}

Type 'MockSummaryService' does not conform to protocol 'ServiceA' Type 'MockSummaryService' does not conform to protocol 'ServiceB'

Kolos65 commented 3 months ago

Hey @VisualDeceit!

Unfortunately protocol inheritance is not supported. This limitation stems from the nature of peer macros: you simply have no access to any information about the protocols you inherit from so there is no way of conforming to them automatically.

Kolos65 commented 3 months ago

I know its not ideal, but you can conform to the inherited protocols in an extension and still use the generated mock implementation for testing:

#if MOCKING
extension MockSummaryService {
    func fooA() {}
    func fooB() {}
}
#endif