Skip to content

Recommended way to mock Kingfisher in tests? #1939

Answered by CraigSiemens
CraigSiemens asked this question in Q&A
Discussion options

You must be logged in to vote

We're using https://github.com/pointfreeco/swift-snapshot-testing to take snapshots of our views in some of our tests. The goal was to have two tests, one to make sure the view had the correct appearance when the image hadn't loaded yet, and another for after the image did load.

What I ended up doing is making a cache in the test and setting the default options on kingfisher to to only use that cache. Something like this.

describe("appearance") {
    beforeEach {
        imageCache = ImageCache(name: self.name)
	
        KingfisherManager.shared.defaultOptions = [
            .onlyFromCache,
            .targetCache(imageCache)
        ]
    }

    afterEach {
        KingfisherManager.sh…

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by onevcat
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants