-
Looking at the Search Demo example, I find this awesome way to replace an implementation: store.assert(
.environment {
$0.weatherClient.searchLocation = { _ in Effect(value: mockLocations) }
},
// ...
) In the The thing to keep notice, in this example, is that the Like this: extension WeatherClient {
static let live = WeatherClient(
searchLocation: { query in
// ...
return URLSession.shared.dataTaskPublisher(for: components.url!)
// ...
},
// ...
} Now, let's say I have this: struct WeatherClient {
var entityClient: () -> EntityClient<LocationModel>
var searchLocation: (AnyHashable, String) -> Effect<[Location], Failure>
struct Failure: Error, Equatable {}
} Where So the question is, how can I access the If I try to replicate the solution from the Search Demo, I get a solution for testing just fine. Just replace the I can't access I can't so this: weatherClient.searchLocation = { id, search in
entityClient().search(search) // Doesn't work, as entityClient is out of scope
} There are 2 solutions that I considered so far: Similar to using a global let some: EntityClient<LocationModel> = CoreDataStack.live.entityClient() I could also, instead of using a pure struct that defines the interface by itself, provide a Protocol like struct LiveWeatherClient: WeatherClientProtocol {
var entityClient: EntityClient<LocationModel>
func searchLocation(id: AnyHashable, search: String) -> Effect<[Location], Failure> {
// ...
}
struct Failure: Error, Equatable {}
} And mock versions: struct MockWeatherClient: WeatherClientProtocol {
// ...
} Has someone thought about this and come up with a solution other than these 2? I'm not yet fully comfortable with the idea of exposing a Am I making this more complicated than it should? 🤔 Apologies for the confusing Title! 🙏 Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi @DiCoDa, I've got a couple of comments and questions for you:
Does any of that help out? |
Beta Was this translation helpful? Give feedback.
-
Great to hear, glad it helped! |
Beta Was this translation helpful? Give feedback.
Hi @DiCoDa,
I've got a couple of comments and questions for you:
You say that you'd like to inject a
URLSession
intoWeatherClient
to make it "testable", but is there some way in which the currentWeatherClient
is not testable? And does injecting aURLSession
into the client somehow unlock more testing abilities that are not currently possible?If you really do need a
URLSession
injected inWeatherClient
, then it can still be done by makinglive
a static function instead of a staticlet
, and passing arguments to the function: