如何使用ohhttpstubs存根在alamofire响应中测试代码

时间:2018-12-29 19:34:13

标签: ios swift alamofire ohhttpstubs

我正在尝试使用ohhttpstubs库测试使用Alamofire的iOS应用。但是,我只能弄清楚如何测试是否发出了请求。我要做的是确保正确执行响应回调并适当更新ViewController。有没有期望或其他方式的直观方法?

到目前为止,我已经尝试过:

测试代码:

    func testFinishedInvitingPeople() {
        let expectation = self.expectation(description: "edit group called")
        stub(condition: isPath("/api/v1/group/1/update_members")) { _ in
        let obj = ["mock": "respones"] as [String: Any]
        expectation.fulfill()
        return OHHTTPStubsResponse(jsonObject: obj, statusCode: 200, headers: nil)
    }
    let people = [Mocks.mockUser, Mocks.mockUser]
    membersTable.finishedInvitingPeople(people)
    waitForExpectations(timeout: 0.3, handler: nil)

我正在测试的代码(RequestBase是我自己的内部类)

    RequestBase(url: "api/v1/group/1/update_members", method: .post, params: params, headers: headers, callback: {response in
        // I want to test the response code in this callback. 
        ...
    })

0 个答案:

没有答案