Created
September 16, 2018 14:30
-
-
Save m25lazi/04aa92109d75b8a3cdc62076e8e675ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Mock Subclass of ImageDownloader | |
/// Fetch API will return what you were passed while init. | |
internal class MockImageDownloader: ImageFetcher { | |
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) | |
} | |
} | |
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") | |
XCTAssertNotNil(currentUser.iProfileImage, "Fetched image should be available in the internal variable.") | |
XCTAssertEqual(currentUser.iProfileImage, currentUser.profileImage, "Fetched image should be available in the public variable when fetch is successful") | |
} | |
// Or even you can do this if ImageFileFetcher is tested separately | |
let imageFileFetcher = ImageFileFetcher(imageName: "timcook.png") | |
currentUser.fetchProfileImage(fetcher: imageFetcher) { (downloaded) in | |
XCTAssertTrue(downloaded, "Downloaded is expected to be true when image is fetched") | |
XCTAssertNotNil(currentUser.iProfileImage, "Fetched image should be available in the internal variable.") | |
XCTAssertEqual(currentUser.iProfileImage, currentUser.profileImage, "Fetched image should be available in the public variable when fetch is successful") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment