Skip to content

Instantly share code, notes, and snippets.

@jayesh15111988
Created May 2, 2023 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayesh15111988/c68505d80a9e928bb70534b69c1cafe8 to your computer and use it in GitHub Desktop.
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
// 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