Created
October 26, 2019 23:22
-
-
Save robertmryan/8989dade9c5bef9ac0db75040d65f747 to your computer and use it in GitHub Desktop.
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
class MyAppTests: XCTestCase { | |
func testIets() { | |
let expectation = self.expectation(description: "doSomething") | |
AsyncClass().doSomething { | |
expectation.fulfill() | |
} | |
wait(for: [expectation], timeout: 5) | |
} | |
} | |
class AsyncClass { | |
func doSomething(completion: @escaping () -> Void) { | |
let group = DispatchGroup() | |
for _ in 0...5 { | |
group.enter() | |
DispatchQueue.global().async { | |
Thread.sleep(forTimeInterval: 1) | |
group.leave() | |
} | |
} | |
group.notify(queue: .main) { | |
completion() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Jasperav - IMHO, it’s not advisable to introduce anti-patterns into one's codebase solely for the sake of testing, when the testing framework already provides a nice, concise mechanism to test these scenarios.
My suggestion is that you do not add blocking/synchronous methods to your code base, and instead use expectations when testing.