Created
May 2, 2023 08:23
-
-
Save jayesh15111988/c68505d80a9e928bb70534b69c1cafe8 to your computer and use it in GitHub Desktop.
A source code on how to implement DispatchQueue mock for unit testing in the iOS app
This file contains 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
// A protocol for mocking DispatchQueue instance | |
protocol DispatchQueueType { | |
func async(execute work: @escaping @convention(block) () -> Void) | |
} | |
// Confirming existing DispatchQueue to DispatchQueueType | |
extension DispatchQueue: DispatchQueueType { | |
func async(execute work: @escaping @convention(block) () -> Void) { | |
async(group: nil, qos: .unspecified, flags: [], execute: work) | |
} | |
} | |
// Mocking dispatch queue for injecting into view model during unit test run | |
final class MockDispatchQueue: DispatchQueueType { | |
func async(execute work: @escaping @convention(block) () -> Void) { | |
work() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment