in EncryptionTests/EncryptionTests.swift [79:112]
func testCreatingAndFetchingContact() {
let finishExpectation = expectation(description: "Expect add and fetch operations to complete")
viewModel.addContact(name: "TestContact-\(UUID().uuidString)",
phoneNumber: "555-123-4567") { [weak self] result in
switch result {
case .failure(let error):
XCTFail("Failed to create new contact: \(error)")
finishExpectation.fulfill()
case .success(let contact):
guard let contact = contact else {
XCTFail("Contact returned from addContact was nil.")
finishExpectation.fulfill()
return
}
self?.contactsToDelete.append(contact)
self?.fetchContactsOrFail { contacts in
finishExpectation.fulfill()
guard let foundContact = contacts.first(where: { $0.id == contact.id }) else {
XCTFail("Created contact not found in subsequent fetch.")
return
}
XCTAssert(foundContact.phoneNumber == contact.phoneNumber,
"Fetched Contact number does not match created Contact number.")
}
}
}
waitForExpectations(timeout: 10)
}