1

Trying to setup a unit test which performs a network request and attempts to serialize the response. I'm currently getting the error: Ambiguous reference to member 'jsonObject(with:options:)'. Confused as to why this is happening as the unit test should know what JSONSerialization. is?

func testAccessKeys() {
    let expected = expectation(description: "Run the Access request")
    sut.request(.Access, data: nil) { finished, response in
        if response != nil && finished == true {
            guard let json = try? JSONSerialization.jsonObject(with: response!, options: .mutableContainers) as! [String:Any] else { return XCTFail("Access request was not a dictionary")}
            XCTAssertNotNil(json?["id"])
            expected.fulfill()
        } else {
            XCTFail("Access response was nil")
        }
    }
    waitForExpectations(timeout: 3) { error in
        if let error = error {
            XCTFail("Access request failure: \(error)")
        }
    }

}
user7684436
  • 697
  • 14
  • 39
  • if you are using Swift 4 it is best to use the new `Codable` protocol and `JSONDecoder` class – Scriptable Apr 09 '18 at 09:44
  • JSONSerialization should work in this instance as only two values will ever be received from this request. – user7684436 Apr 09 '18 at 09:47
  • as long as response is of type Data then this should work, it looks like the right syntax. make sure that you have imported foundation at the top of your test, what swift version are you using? – Scriptable Apr 09 '18 at 09:50
  • That was it, had mapped response as Any! Thanks – user7684436 Apr 09 '18 at 09:59

1 Answers1

1

Make sure that response is of type Data or InputStream.

These are the only types that are accepted by this function as you can see in the documentation

Scriptable
  • 19,402
  • 5
  • 56
  • 72