简体   繁体   English

Xcode 8 / Swift 3 中的 iOS 异步单元测试(waitForExpectations 失败)

[英]iOS Asynchronous Unit Testing (waitForExpectations failing) in Xcode 8 / Swift 3

I'm updating my Cocoapod that I developed to use Swift 3. Before I upgraded, all of my asynchronous unit tests were working perfectly fine.我正在更新我为使用 Swift 3 而开发的 Cocoapod。在升级之前,我所有的异步单元测试都运行良好。 But now after upgrading, every single one of them immediately fail and I have no idea why.但是现在升级后,每一个都立即失败,我不知道为什么。 Here is what each one is structured like:以下是每一个的结构:

override func setUp() {
        super.setUp()
        validationExpectation = expectation(description: "Validation")
}

.
.
.

func testSymbolRequest(){

        _ = MyCocoapod.makeSymbolRequest(symbol: "ABC", success: { (symbolObject) in
            self.validationExpectation.fulfill()
            XCTAssert(true)
        }) { (error) in
            self.validationExpectation.fulfill()
            XCTFail(error.description)
        }

        waitForRequestToFinish()
}

.
.
.

func waitForRequestToFinish(){
    waitForExpectations(timeout: 60.0) { (error) in
        if let error = error {
            XCTFail(error.localizedDescription)
        }
    }
}

The waitForExpectations function isn't waiting at all. waitForExpectations 函数根本不等待。 It immediately fails after being called.它在被调用后立即失败。 I have also confirmed that is has nothing to do with my actual networking code and the requests work perfectly fine in my Cocoapod example project.我还确认这与我的实际网络代码无关,并且请求在我的 Cocoapod 示例项目中工作得非常好。 I'm using Alamofire.我正在使用 Alamofire。 I don't think that's the problem but I thought it might be worth mentioning.我不认为这是问题所在,但我认为这可能值得一提。 The error message that is printed out is "unknown error".打印出来的错误信息是“未知错误”。

If you are trying to test your CocoaPod that you are developing, for some reason, the default testing target that it creates makes the waitForExpectations() function not work properly for that particular testing target.如果您正在尝试测试您正在开发的 CocoaPod,出于某种原因,它创建的默认测试目标会使 waitForExpectations() 函数无法针对该特定测试目标正常工作。 I was able to get it to finally work by doing these steps in order:通过按顺序执行以下步骤,我能够使其最终工作:

  1. Delete the current testing target删除当前测试目标
  2. Create a new testing target创建一个新的测试目标
  3. Run a pod install and make sure the pod file is updated accordingly运行 pod install 并确保相应地更新 pod 文件

Following these steps, I was able to get the waitForExpectations() to finally work within my network requests.按照这些步骤,我能够让 waitForExpectations() 最终在我的网络请求中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM