You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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() {}
}
However sometimes the output is correct:
public class Bar: Foo {
public init() {}
public var barCallsCount = 0
public var barCalled: Bool { barCallsCount > 0 }
public var barReturnValue: AnyPublisher<[String], Never>!
public var barClosure: (() -> AnyPublisher<[String], Never>)?
public func bar() -> AnyPublisher<[String], Never> {
barCallsCount += 1
if let barClosure = barClosure {
return barClosure()
} else {
return barReturnValue
}
}
}
How can I ensure Sourcery consistently generates the correct mock for protocols with associated types and type constraints?
Sourcery: 2.2.6
Xcode: 16.1
Language: Swift 6
The text was updated successfully, but these errors were encountered:
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.
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:
Output:
However sometimes the output is correct:
How can I ensure Sourcery consistently generates the correct mock for protocols with associated types and type constraints?
Sourcery:
2.2.6
Xcode:
16.1
Language:
Swift 6
The text was updated successfully, but these errors were encountered: