Skip to content

Instantly share code, notes, and snippets.

@MounikaMadishetti
Created March 3, 2024 06:33
Show Gist options
  • Save MounikaMadishetti/c9e935ef2dd8711ac03e83bb9563c4c1 to your computer and use it in GitHub Desktop.
Save MounikaMadishetti/c9e935ef2dd8711ac03e83bb9563c4c1 to your computer and use it in GitHub Desktop.
// MARK: - Main Target
protocol HTTPSession {
func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> HTTPSessionTask
}
protocol HTTPSessionTask {
func resume()
}
final class HTTPClient {
let session: HTTPSession
init(session: HTTPSession) {
self.session = session
}
func get(from url: URL) {
session.dataTask(with: URLRequest(url: url)).resume()
}
}
// MARK: - Test Target
final class HTTPClientTests {
func test_initHTTPClient_sessionNotNil() {
let sut = makeSUT()
XCTAssertNotNil(sut.session)
}
func makeSUT() -> HTTPClient {
return HTTPClient(session: URLSessionSpy())
}
}
final class URLSessionSpy: HTTPSession {
func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> HTTPSessionTask {
completionHandler(nil, nil, nil)
return SampleURLSessionDataTask()
}
private class SampleURLSessionDataTask: HTTPSessionTask {
func resume() {
print("resume called")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment