Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 异步服务调用的TableView单元测试_Ios_Objective C_Swift - Fatal编程技术网

Ios 异步服务调用的TableView单元测试

Ios 异步服务调用的TableView单元测试,ios,objective-c,swift,Ios,Objective C,Swift,我正在为具有UITableView的应用程序编写单元测试用例,数据来自服务器。除了numberofrowsinssection之外,我已经为我的TableView添加了测试用例 下面是UITableView测试用例的链接: 有人能建议如何为显示来自异步服务调用的数据的numberofrowsinssection编写测试用例吗? 任何想法或示例都会非常有用。您可以使用json来伪造数据。将带有面数据的json文件添加到项目中(请小心选择正确的目标)。然后使用下一个代码测试数据: import X

我正在为具有UITableView的应用程序编写单元测试用例,数据来自服务器。除了
numberofrowsinssection
之外,我已经为我的TableView添加了测试用例

下面是UITableView测试用例的链接:

有人能建议如何为显示来自异步服务调用的数据的
numberofrowsinssection
编写测试用例吗? 任何想法或示例都会非常有用。

您可以使用json来伪造数据。将带有面数据的json文件添加到项目中(请小心选择正确的目标)。然后使用下一个代码测试数据:

import XCTest
import OHHTTPStubs

class TestSomeRequest: XCTestCase {

    // MARK: - Attributes
    fileprivate let endpoint = "yourendpoint"
    fileprivate let apiUrl = "yoururl"
    fileprivate let path = "yourpath"
}


// MARK: - Setup & Tear Down
extension TestSomeRequest {
    override func setUp() {
        super.setUp()
        stub(condition: isHost((URL(string: apiUrl)?.host)!) && isPath(path), response: {_ in
            guard let path = OHPathForFile("TestDataJson.json", type(of: self)) else {
                preconditionFailure("Could Not Find Test File!")
            }
            return OHHTTPStubsResponse(fileAtPath: path, statusCode: 200, headers: ["Content-Type": "application/json"])
        })
    }

    override func tearDown() {
        super.tearDown()
        OHHTTPStubs.removeAllStubs()
    }
}


// MARK: - Tests
extension TestSomeRequest {
    func testNumberOfRowsInSection() {
        let fetchExpectation = expectation(description: "Test Fetching")
        let viewController = YourViewController()
        YourDataManager.shared.fetchData(for: endpoint, success: {
            XCTAssertEqual(viewController.tableView.numberOfRows(inSection: 0), expectedNumberOfRows, "Number Of Rows In Section 0 Should Match!")
            fetchExpectation.fulfill()
        }, failure: nil)
        waitForExpectations(timeout: 60, handler: nil)
    }
}
如果您不想伪造数据,请使用以下测试方法:

func testNumberOfRowsInSection() {
    let fetchExpectation = expectation(description: "Test Fetching")
    let viewController = YourViewController()
    YourDataManager.shared.fetchData(for: endpoint, success: {
        XCTAssertEqual(viewController.tableView.numberOfRows(inSection: 0), expectedNumberOfRows, "Number Of Rows In Section 0 Should Match!")
        fetchExpectation.fulfill()
    }, failure: nil)
    waitForExpectations(timeout: 60, handler: nil)
}