Mockingjay icon indicating copy to clipboard operation
Mockingjay copied to clipboard

Custom matcher runs twice if matcher returns true

Open trinhhunganh opened this issue 6 years ago • 0 comments

I am running stub request test in XCTest and my case is as following:

func testExample() {
        func matchPath(request: URLRequest) -> Bool {
            let path = request.url?.path
            return path != nil // setup a breakpoint on this line and you can see it runs twice
        }

        var responseBodyJson: [String: String] = [:]
        responseBodyJson["appId"] = "1"
        stub(matchPath, json(responseBodyJson, status: 200, headers: nil))
        
        let urlPath: String = "http://www.stackoverflow.com"
        let url: URL = URL(string: urlPath)!
        let request1: URLRequest = URLRequest(url: url)
        let response: AutoreleasingUnsafeMutablePointer<URLResponse?>? = nil
        do{
            let dataVal = try NSURLConnection.sendSynchronousRequest(request1, returning: response)
            print(response)
            do {
                if let jsonResult = try JSONSerialization.jsonObject(with: dataVal, options: []) as? NSDictionary {
                    print("Synchronous\(jsonResult)")
                }
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }catch let error as NSError
        {
            print(error.localizedDescription)
        }
}

trinhhunganh avatar Sep 10 '19 10:09 trinhhunganh