gpt4 book ai didi

ios - XCTest:传递函数参数

转载 作者:行者123 更新时间:2023-11-28 11:16:02 24 4
gpt4 key购买 nike

从 XCTest 文件中,我调用了一个 Web 服务调用,并使用“self.waitForExpectationsWithTimeout”API 放置了一个等待 block 并获得服务响应。

我有几个测试方法要用这个服务响应来执行。当我将此响应存储在全局变量中并从其他测试函数访问它时,此变量为 nil。这里需要做什么?我可以将它作为函数参数传递吗?

let serviceResp :NSDictionary!

func test_One() {
//let expectation: XCTestExpectation = self.expectationWithDescription("HTTP")

datamanager.fetchData() //Web service

self.waitForExpectationsWithTimeout(5, handler: { (error: NSError!) -> Void in

//In 5 seconds, I will get response from service and will be stored in datamanager.response.

self.serviceResp = datamanager.response
})
}


func test_Two() {
//self.serviceResp is coming as nil even after assigning a value to it.
}

谢谢

最佳答案

您不能以这种方式在 XCTest 方法之间传递信息。从 Testing with Xcode Docs 查看以下内容(强调我的)

For each class, testing starts by running the class setup method. For each test method, a new instance of the class is allocated and its instance setup method executed. After that it runs the test method, and after that the instance teardown method. This sequence repeats for all the test methods in the class. After the last test method teardown in the class has been run, Xcode executes the class teardown method and moves on to the next class. This sequence repeats until all the test methods in all test classes have been run.

如果有您的所有测试都需要运行的信息,请考虑将其放入设置方法中。

关于ios - XCTest:传递函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31731497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com