There are a few ways to make XCTest wait for async calls in setUp, depending on the specific scenario. Here are three possible methods:
Create a XCTestExpectation object in setUp and fulfill it in the completion block of the async call. Wait for the expectation to be fulfilled before running tests using the wait(for:timeout:) method.
Example code:
func testExample() {
// ...
}
override func setUp() {
super.setUp()
let expectation = XCTestExpectation(description: "Async call completion")
asyncCall {
expectation.fulfill()
}
wait(for: [expectation], timeout: 5.0)
}
Create a DispatchGroup in setUp and enter it before the async call. Leave the group in the completion block of the async call. Wait for the group to be empty before running tests using the wait() method.
Example code:
func testExample() {
// ...
}
override func setUp() {
super.setUp()
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
asyncCall {
// ...
dispatchGroup.leave()
}
dispatchGroup.wait(timeout: DispatchTime.now() + 5.0)
}
Modify the asyncCall method to accept a completion handler as a parameter. Call the completion handler in the completion block of the async call. Wait for the async call to complete before running tests using the setUp completion handler.
Example code:
func testExample() {
// ...
}
override func setUp() {
super.setUp()
let expectation = XCTestExpectation(description: "Async call completion")
asyncCallWithCompletionHandler {
expectation.fulfill()
}
wait(for: [expectation], timeout: 5.0)
}
func asyncCallWithCompletionHandler(completion: @escaping () -> Void) {
// ...
asyncCall {
// ...
completion()
}
}
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-03 14:30:24 +0000
Seen: 14 times
Last updated: Jun 03 '23
What is the method for altering the color of the input text in a TextFormField in Flutter?
What is the procedure for utilizing the node-rdpjs library?
What is the method to change a PDF file to an Excel file using C#?
What is the method to make a TextButton inactive when the text field has no text?
What is the method to alter the background image in HTML?
What is the method for saving an entity with @EmbeddedId as its primary key in Hibernate?
What is the method to superimpose two seaborn density plots?