Trying to unit test a network call and getting the following error:
API violation - multiple calls made to -[XCTestExpectation fulfill]
If written this style of testing before, but this one seems to be creating the error and I can't seem to figure out why.
func testAccessKeys() {
let expected = expectation(description: "Run the Access request")
sut.request(.Access, data: nil) { finished, response in
if response != nil && finished == true {
if let json = response as? [String:Any] {
if json["id"] as? String != nil, json["secret"] as? String != nil {
XCTAssertNotNil(json)
expected.fulfill()
} else {
XCTFail("Access response was not in correct format")
expected.fulfill()
}
} else {
XCTFail("Access request was not a dictionary")
expected.fulfill()
}
} else {
XCTFail("Access response was nil")
expected.fulfill()
}
}
waitForExpectations(timeout: 3) { error in
if let error = error {
XCTFail("Access request failure: \(error.localizedDescription)")
}
}
}
UPDATE Simplifying the call solved the problem:
func testAccessKeys() {
let expected = expectation(description: "Run the Access request")
sut.request(.Access, data: nil) { finished, response in
if response != nil && finished == true {
if let json = response as? [String:Any] {
if json["id"] as? String != nil, json["secret"] as? String != nil {
print("ID: \(String(describing: json["id"]))")
print("Secret: \(String(describing: json["secret"]))")
expected.fulfill()
} else {
XCTFail("Access response was not in correct format")
}
} else {
XCTFail("Access request was not a dictionary")
}
} else {
XCTFail("Access response was nil")
}
}
waitForExpectations(timeout: 3, handler: nil)
}