Unit test for method that call Async methods

Viewed 2517

supposed I have these lines of code:

func reset() {
    initializeAnythingElse() {
        // AnythingElse
    }

    initializeHomeData() {
        // HomeData
    }
}

func initializeHomeData(callback: @escaping (()-> Void)) {
    getHomeConfig() {
        callback()
    }
}

func initializeAnythingElse(callback: @escaping (()-> Void)) {
    getAnythingElse() {
        callback()
    }
}

and I would like to write a unit test for that code. For initializeHomeData and initializeAnythingElse, I can write the unit test like :

func testInitializeHomeData() {
    let successExpectation = expectation(description: "")

    sut.initializeHomeData {
        successExpectation.fulfill()
    }

    waitForExpectations(timeout: 1.0, handler: nil)

    // Validation goes here
}

func testInitializeAnythingElse() {
    let successExpectation = expectation(description: "")

    sut.initializeAnythingElse {
        successExpectation.fulfill()
    }

    waitForExpectations(timeout: 1.0, handler: nil)

    // Validation goes here
}

My question is, how to test reset()? Should I just call them inside testReset() like :

func testReset() {
    testInitializeHomeData()
    testInitializeAnythingElse()
}

but I think this is not the proper implementation for that.

3 Answers
Related