title: How to mock DataTaskPublisher?
body: <p>I'm trying to write some unit tests for my API using URLSession.DataTaskPublisher. I've found an already existing question on Stackoverflow for the same but I'm struggling to implement a working class using the proposed solution.</p>
  
  <p>Here's the existing question: <a href="https://stackoverflow.com/questions/59521928/how-to-mock-urlsession-datataskpublisher">How to mock URLSession.DataTaskPublisher</a></p>
  
  <pre><code>protocol APIDataTaskPublisher {
      func dataTaskPublisher(for request: URLRequest) -&gt; URLSession.DataTaskPublisher
  }
  
  class APISessionDataTaskPublisher: APIDataTaskPublisher {
      func dataTaskPublisher(for request: URLRequest) -&gt; URLSession.DataTaskPublisher {
          return session.dataTaskPublisher(for: request)
      }
  
      var session: URLSession
  
      init(session: URLSession = URLSession.shared) {
          self.session = session
      }
  }
  
  class URLSessionMock: APIDataTaskPublisher {
      func dataTaskPublisher(for request: URLRequest) -&gt; URLSession.DataTaskPublisher {
          // How can I return a mocked URLSession.DataTaskPublisher here?
      }
  }
  </code></pre>
  
  <p>My API then uses the above like this:</p>
  
  <pre><code>class MyAPI {
      /// Shared URL session
      private let urlSession: APIDataTaskPublisher
  
      init(urlSession: APIDataTaskPublisher = APISessionDataTaskPublisher(session: URLSession.shared)) {
          self.urlSession = urlSession
      }
  }
  </code></pre>
  
  <p>What I don't know is how to implement URLSessionMock.dataTaskPublisher().</p>