Closed kelvinharron closed 2 months ago
The type system must be able to infer all generic parameters and return values so in this case you are limited to using willProduce
and need to specify the return value in the closure signature:
given(baseAPIClient)
.fetch(using: .any)
.willProduce { _ -> String in
throw TestError.generic
}
do {
let url = URL(string: "apple.com")!
let request = URLRequest(url: url)
let _: String = try await mock.fetch(using: request)
} catch {
XCTAssertEqual(error as! TestError, TestError.generic)
}
You're a saint Kolos, thank you! I found that if my tested code is calling a mocked object like
let authenticationDataResult: AuthenticationResponse = try await apiClient.fetch(using: loginRequest)
Then I need to define the mock as
given(apiClient)
.fetch(using: .any)
.willProduce { _ -> AuthenticationResponse in
throw ApiError.httpResponseFailure
}
where it works perfectly! Would you welcome a pull request to note this use case in the documentation?
I will add a "working with generics" section where I would list this as well as other edge cases and limitations regarding generics.
Glad I could help @kelvinharron 🚀
I have one blocker that might be a limitation of my implementation, so I'd like to sanity check it here. In our project, we have the following BaseAPIClient protocol.
Implementers of the class declare the BaseAPIClient type and explicitly define the `T: Codable`` return type, we get a very flexible and reusable client.
The problem is the use of given in the mock is unable to compile when I want to define a
willThrow
for it.Have you encountered this before @Kolos65 and have any recommendations as a work around? Or is there a limitation I could help contribute a resolution towards? The alternative approach to my code is to provide an associatedType in the protocol and define that as the response without generics, which means explicit API clients that do duplicating the logic around handling the various HTTP response and error handling. I have attached an example project to help illustrate the issue. Thank you.
MockableGenericExample.zip