Skip to content

Instantly share code, notes, and snippets.

@m25lazi
Created September 16, 2018 14:06
Show Gist options
  • Save m25lazi/a63fcf3065c6b9f961ffaaf94e7a85c8 to your computer and use it in GitHub Desktop.
Save m25lazi/a63fcf3065c6b9f961ffaaf94e7a85c8 to your computer and use it in GitHub Desktop.
/// Mock Subclass of ImageDownloader
/// Fetch API will return what you were passed while init.
internal class MockImageDownloader: ImageDownloader {
typealias MockResponse = (UIImage?, Error?)
/// Mocked response. Pass this as part of init and this will be the response of fetch API
let mockedResponse: MockResponse
init(url: URL, response: MockResponse) {
mockedResponse = response
super.init(url: url)
}
override func fetch(then completion: @escaping (UIImage?, Error?) -> Void) {
completion(mockedResponse.0, mockedResponse.1)
}
}
// Note: You should have created XCTestExpectation and wait for response, since this is an asynchronous API testing
let currentUser = User(name: "Tim Cook")
let imageFetcher = MockImageDownloader(url: URL(string: "https://somerandomapp.mlaz.im/images/GkdhaKGjhgj7yjHo")!, response: (UIImage(named: "timcook.png"), nil))
currentUser.fetchProfileImage(fetcher: imageFetcher) { (downloaded) in
XCTAssertTrue(downloaded, "Downloaded is expected to be true when image is fetched")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment