迅速 – 在单元测试中等待Alamofire
发布时间:2020-12-14 04:29:12 所属栏目:百科 来源:网络整理
导读:我正在尝试编写一种方法,其中数据对象(Realm)使用Alamofire刷新它的属性.但我无法弄清楚如何对它进行单元测试. import Alamofireimport RealmSwiftimport SwiftyJSONclass Thingy: Object { // some properties dynamic var property // refresh instance fu
我正在尝试编写一种方法,其中数据对象(Realm)使用Alamofire刷新它的属性.但我无法弄清楚如何对它进行单元测试.
import Alamofire import RealmSwift import SwiftyJSON class Thingy: Object { // some properties dynamic var property // refresh instance func refreshThingy() { Alamofire.request(.GET,URL) .responseJSON { response in self.property = response["JSON"].string } } } 在我的单元测试中,我想测试Thingy是否可以正常刷新服务器. import Alamofire import SwiftyJSON import XCTest @testable import MyModule class Thingy_Tests: XCTestCase { func testRefreshThingy() { let testThingy: Thingy = Thingy.init() testThingy.refreshProject() XCTAssertEqual(testThingy.property,expected property) } 如何为此正确设置单元测试? 解决方法
使用XCTestExpectation等待异步进程,例如:
func testExample() { let e = expectation(description: "Alamofire") Alamofire.request(urlString) .response { response in XCTAssertNil(response.error,"Whoops,error (response.error!.localizedDescription)") XCTAssertNotNil(response,"No response") XCTAssertEqual(response.response?.statusCode ?? 0,200,"Status code not 200") e.fulfill() } waitForExpectations(timeout: 5.0,handler: nil) } 在您的情况下,如果您要测试异步方法,则必须为refreshThingy提供完成处理程序: class Thingy { var property: String! func refreshThingy(completionHandler: ((String?) -> Void)?) { Alamofire.request(someURL) .responseJSON { response in if let json = response.result.value as? [String: String] { completionHandler?(json["JSON"]) } else { completionHandler?(nil) } } } } 然后你可以测试Thingy: func testThingy() { let e = expectation(description: "Thingy") let thingy = Thingy() thingy.refreshThingy { string in XCTAssertNotNil(string,"Expected non-nil string") e.fulfill() } waitForExpectations(timeout: 5.0,handler: nil) } 坦率地说,无论如何,这种使用完成处理程序的模式可能是你想要在refreshThingy中使用的模式,但是如果你可能不想提供一个完成处理程序,我会把它作为可选项. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |