Swift: Mock an Image Download for Unit Testing

I'm testing an image cache function. I pass a string url to it and it caches that image along with its associated url.

For testing purpose, I want to remove the dependency on the network call. Is it possible to mock an image download?

This is the function:

imageView.imageFromUrl(imageUrl!)

I tried passing it a local file url. But it didn't work because the imageFromUrl doesn't work with local urls.

Is it possible to mock downloading an image?

let imageUrl = Bundle.main.path(forResource: "testImage", ofType: "png")
        print(imageUrl!)

1 answer

  • answered 2019-07-11 04:57 Mutaeb Alqahtani

    NO, you can't mock an image download!