Generate generic mocks for protocols with associated types
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 886
- Forks
- 102
- Avg merge
- 6h 36m
- Merged PRs (30d)
- 2
Description
Given the following protocol:
protocol Foo<Output> {
associatedtype Output
func fetch() -> Output
}
The current generated mock uses Any for the associated type. This makes it awkward to use the mock, because we need to typecast it at the usage site since Any is usually not the concrete type we want. I know we can specify a concrete type using /// @mockable(typealias: Output = String), but that defeats the purpose of the associated type by limiting it to a single concrete type.
Current
class FooMock: Foo {
init() { }
typealias Output = Any
private(set) var fetchCallCount = 0
var fetchHandler: (() async throws -> Output)?
func fetch() async throws -> Output {
fetchCallCount += 1
if let fetchHandler = fetchHandler {
return try await fetchHandler()
}
fatalError("fetchHandler returns can't have a default value thus its handler must be set")
}
}
Expected
Would it instead be possible to generate a mock like this?
class FooMock<T>: Foo {
init() { }
typealias Output = T
// ... etc
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing Mockolo's protocol mock generation for the associated-type example in the issue; the payload does not name specific source files or tests. Define done as generating a generic mock whose type parameter supplies the associated type, while preserving the existing handler and call-count behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- devtools, testing
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100