首页 > 解决方案 > API 测试中对成员的模糊引用

问题描述

我正在努力学习Unit Testing。为此,我想测试来自 API 的响应。这是我的代码

func testAPIworking(){
        guard let url = URL(string: profileUrlstring) else {fatalError()}
        var request = URLRequest(url: url)
        request.httpMethod = "GET"

        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in

            if error == nil {
                XCTFail("Fail")
            }
            expectation.fulfill() //I got an error here
        }
        self.waitForExpectations(timeout: 20)
    }

但我收到了这个错误:

在行中对成员“期望(描述:)”的模糊引用

expectation.fulfill()

谁能帮我看看我的问题在哪里?非常感谢

标签: swiftrestunit-testing

解决方案


推荐阅读