使用URLRequestConvertible链接单元测试中的错误 [英] Linking error in unit test using URLRequestConvertible
问题描述
我有一个奇怪的问题,编写一个简单的单元测试(Xcode 7.2),这是一个非常简单的函数,它确保一个参数被添加到一个URL:
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
}
单元测试是这样的:
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)
当我从正常目标运行它时,该函数有效,所以问题在于测试函数,但我不清楚关于我应该如何调用它,我在非测试类中运行相同的代码并且没有问题,所以我对此有点困惑。此错误消息有点神秘,从我用Google搜索,见证表有点像协议的vtable。但我没有搞砸了, NSMutableURLRequest
的扩展是在Alamofire中实现的,而我只是在使用它。
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?
推荐答案
确保Alamofire库与您的测试目标链接。
Ensure the Alamofire library is linked with your test target.
您可能还需要删除继承! :来自测试目标的search_paths
。
这篇关于使用URLRequestConvertible链接单元测试中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!