I use Sourcery in my iOS project to generate mocks for a protocol
that conforms to another protocol
with associated types. Despite consistent input, the generated output from Sourcery is not consistent. Sometimes the output is correct, but other times it's incorrect, defining Mock as a class
with only init
.
Here is my AutoMockable.stencil: https://gist.github.com/levan9999/212647d48ef372e025d5794543ab303c
Here’s my setup:
public protocol Foo {
associatedtype Input
associatedtype Output
associatedtype Failure: Error
@discardableResult
func bar(parameters: Input) -> AnyPublisher<Output, Failure>
}
// sourcery: AutoMockable
public protocol Bar: Foo
where Input == String,
Output == Bool,
Failure == Never { }
Output:
public class Bar: Foo {
public init() {}
}
How can I ensure Sourcery consistently generates the correct mock for protocols with associated types and type constraints?
After some investigation, I identified the problem: I use modular architecture and the issue was that Sourcery couldn't resolve a parent protocol located in another module when generating mocks for a child protocol in the current module. Since Sourcery processes only the specified source files, it lacked the necessary context for the parent protocol.