Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

swift - Waiting for Alamofire in Unit Tests

I'm trying to write a method where a data object (Realm) refreshes it's properties using Alamofire. But I can't figure out how to unit test it.

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
        }
    }
}

In my unit tests, I want to test that the Thingy can refresh from server properly.

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)
}

How do I properly set up unit tests for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use XCTestExpectation to wait for asynchronous processes, for example:

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)
}

In your case, if you're going to test your asynchronous method, you have to provide a completion handler to 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)
                }
        }
    }
}

Then you can test 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)
}

Frankly, this pattern of using a completion handler is probably something that you want in your refreshThingy, anyway, but I made it optional in case you might not want to supply a completion handler.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...