【问题标题】:Waiting for Alamofire in Unit Tests在单元测试中等待 Alamofire
【发布时间】:2016-03-20 14:02:55
【问题描述】:

我正在尝试编写一个方法,其中数据对象(领域)使用 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)
}

如何为此正确设置单元测试?

【问题讨论】:

    标签: swift unit-testing alamofire


    【解决方案1】:

    使用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,但我将其设为可选,以防您可能不想提供完成处理程序。

    【讨论】:

    • 这正是我想要的。谢谢罗!
    • 漂亮的尾随闭包语法,而不是thingy.refreshThingy { (string:String?)->() in
    • Rob,你为什么把5.0秒放在timeout而不是1.0
    • @LorenzoBoaro - 您需要一个足够大的值来避免误报。在我的环境中,网络延迟偶尔会(尽管很少)超过 1 秒。而且使用大超时通常没有什么缺点,因为它通常成功或失败比这快得多,并且永远不会达到超时。
    • @Rob 模拟网络请求是否会更好(在您看来),以便它可以同步返回而根本不必等待网络?我意识到你必须将虚拟数据/JSON 传回给请求者,但这比使用实际服务更好吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    相关资源
    最近更新 更多