Skip to content

How to inject a dependency in an Environment's dependency, in such way that it is testable? #364

Answered by mbrandonw
ghost asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @DiCoDa,

I've got a couple of comments and questions for you:

  • You say that you'd like to inject a URLSession into WeatherClient to make it "testable", but is there some way in which the current WeatherClient is not testable? And does injecting a URLSession into the client somehow unlock more testing abilities that are not currently possible?

  • If you really do need a URLSession injected in WeatherClient, then it can still be done by making live a static function instead of a static let, and passing arguments to the function:

    extension WeatherClient {
      static func live(session: URLSession = .shared) -> Self {
        Self(
          searchLocation: { id, query in 
            // use URLSession i…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@ghost
Comment options

Answer selected
Comment options

You must be logged in to vote
4 replies
@swasta
Comment options

@siejkowski
Comment options

@ghost
Comment options

@swasta
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants