在XCTestCase中使用URL方案

McZ*_*onk 8 macos xcode objective-c xctest swift

我想运行一个等待浏览器响应的测试.

测试目标有一个Info.plist,我可以在其中注册自定义URL方案.但那些从未被称为.我知道测试目标不是真正的应用程序.

有办法吗?

编辑(用于赏金):我想为一个调用的类编写一个集成测试openUrl("tel://" + some phone number).如何在XCTestCase中订阅此URL方案?

Pri*_*ngo -1

我不确定你到底想测试什么。我认为这不仅仅是 URL 是否可以打开的问题。下面的测试用例检查 FaceTime 是否会打开以及是否可以打开 URL。如果您正在测试的应用程序注册了自定义方案,那么只需更改pathToTestApplication即可。

在此输入图像描述

class NSURL_SchemeTests: XCTestCase {

    func testWhoHandlesSchemeTel() {
        let testAppURL = NSURL(string: pathToTestApplication)

        // create a NSURL for the test
        let tel = "tel:5555555555"
        let url = NSURL(string: tel)
        XCTAssert(url != nil, "Could not create a URL from \(tel)")

        // determine whether any application is registered to handle the URL
        let workspace  = NSWorkspace.sharedWorkspace()
        let urlHandler = workspace.URLForApplicationToOpenURL(url!)
        XCTAssert(urlHandler != nil, "No application found to open URL \(tel)")

        // determine whether the expected test application is registered to handle the URL
        XCTAssert(urlHandler! == testAppURL,
            "Application \(pathToTestApplication) was not registered to handle URL \(tel)")

        // attempt to open the URL
        XCTAssert(workspace.openURL(url!), "Could not open URL \(tel)")
    }
}
Run Code Online (Sandbox Code Playgroud)