在Swift中使用OHHTTPStubs模拟Facebook GraphRequest

时间:2018-12-27 16:46:01

标签: ios swift facebook-graph-api ohhttpstubs

我一直在尝试构建一个使用FBSDKGraphRequest类的应用程序,并且我正在尝试测试它对该类的调用。我正在使用OHHTTPStubs,但是我不知道指定模拟FBSDKGraphRequest的路径。如何截取和模拟FBSDKGraphRequest?

相关代码:

我要模拟的图形请求

    let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "events"])
    graphRequest.start { (_, result, error) in
        if let error = error {
            print(error.localizedDescription)
        } else {
            if FBSDKAccessToken.current() != nil {
                //process response
            }
        }
    }

测试用例

func testSyncFBEvents() {
    let callFacebookAPI = self.expectation(description: "fb triggers")
    stub(condition: isPath("/me")) { _ in
        let obj = ["mock": "responses"] as [String: Any]
        callFacebookAPI.fulfill()
        return OHHTTPStubsResponse(jsonObject: obj, statusCode: 200, headers: nil)
    }
    appDelegate.callFacebookGraphPath()
    waitForExpectations(timeout: 5, handler: nil)
}

0 个答案:

没有答案