Search code examples
iosswiftunit-testingxcode7alamofire

Linking error in unit test using URLRequestConvertible


I have a weird problem writing a simple unit test (Xcode 7.2) for a very simple function which makes sure a parameter is added to a URL:

func appendToken(token: String, toRequest request: URLRequestConvertible) throws -> URLRequestConvertible {
    var error: NSError?
    let modifiedRequest: NSMutableURLRequest
    (modifiedRequest, error) = Alamofire.ParameterEncoding.URL.encode(request, parameters: ["token": self.token])

    guard error == nil else {
        // TODO: handle error
        throw error!
    }

    return modifiedRequest
}

The unit test is like this:

func testTokenAddition() {
    let token = "ABCD12345"
    let client = MyClass(token: token)
    let originalRequest = NSURLRequest(URL: NSURL(string:"http://localhost")!)

    do {
        let modifiedRequest = try client.appendToken(token, toRequest: originalRequest).URLRequest
        XCTAssertTrue(modifiedRequest.URLRequest.URLString.hasSuffix("token=\(token)"))
    } catch {
        XCTFail()
        print(error)
    }
}

Obviously this is a very simple test on which I want to build upon (so please don't focus on the actual assertion). But when I try to run the test, I get this linking error:

Undefined symbols for architecture x86_64:
  "protocol witness table for __ObjC.NSURLRequest : Alamofire.URLRequestConvertible in Alamofire", referenced from:
  SDKitTests.SDKitTests.testTokenAddition (SDKitTests.SDKitTests)() -> () in SDKitTests.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The function works when I run it from a normal target, so the problem is in the test function, but I'm not clear on how I should be calling it, I run the same code in a non-test class and there was no problem, so I'm a bit puzzled by this. Also the error message is a bit cryptic, from what I've Googled, a witness table is kind of like a vtable for protocols. But I haven't messed with that, the extension to NSMutableURLRequest is implemented in Alamofire, and I'm just using it.

What am I doing wrong? What do I need to do to fix it?


Solution

  • Ensure the Alamofire library is linked with your test target.

    You may also need to remove inherit! :search_paths from the test target.