【问题标题】:Quickspec test never gets executed successfully when the test includes an HTTP request当测试包含 HTTP 请求时,Quickspec 测试永远不会成功执行
【发布时间】:2018-09-10 23:06:41
【问题描述】:

我目前正在为我正在开发的 OSX 框架的一小部分编写测试。测试是使用包QuickNimble 编写的。测试本身非常基础:

class OrderbookTestKraken: QuickSpec {
    let kraken = Kraken()

    override func spec() {
        let coin = Coin(name: "DOGE", ask: 0.0, bid: 0.0, last: 0.0, bidSize: 0.0, askSize: 0.0)
        describe("When an order book is requested") {
            it("it should create an orderbook successfully") {
                self.kraken.getOrderbook(coin: coin) { (error, orderbook) in
                    expect(orderbook).notTo(beNil())
                }
            }
        }
    }
}

每当我运行它时,测试都会立即通过。即使有:

1) 我的Kraken 实例的getOrderbook 方法中的断点。 2) nilorderbook 变量中返回,该变量在 expect 语句中使用。

getOrderbook 方法如下所示:

func getOrderbook(coin: Coin, onCompletion: @escaping (Error?, Orderbook?) -> Void) {
    Alamofire.request("\(publicApiUrl ?? "")/Depth",
        method: .get,
        parameters: ["pair": "\(coin.name)XBT", "count": 3],
        encoding: URLEncoding.default,
        headers: nil).validate().responseJSON { response in
        // There's a breakpoint here.
        onCompletion(nil, nil)
    }
}

所以,它总是在回调中返回nil,并且有一个未触发的断点,但测试总是成功。当我尝试一些穴居人调试,并在Alamofire 请求上方放置一个断点时,断点确实被触发了。

这是某个已知问题,还是我正在监督的问题?

谢谢。

【问题讨论】:

  • 您是否在控制台中看到实际开始并通过了测试?
  • @Cristik 是的。它说它通过了。

标签: swift unit-testing alamofire quick-nimble


【解决方案1】:

我认为您将 nil 作为参数值传递给 onCompletion 。这就是为什么总是收到 nil 作为响应。尝试传递有效的参数值而不是 nil

例如:

onCompletion(err, params)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    相关资源
    最近更新 更多