Testing in iOS - Part 14: XCTWaiter and Expectations | Ray Wenderlich

The XCTWaiter and expectations allow you to wait for a result in your unit tests.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3530-testing-in-ios/lessons/14

This part fails for me but succeeds in the video. XCTAssert(result == XCTWaiter.Result.completed, “Loading content for Info View did not change text”)

Here is my code for this part:

// testing data that could be delayed
func testInfoLoading() {
    let sb = UIStoryboard(name: "Main", bundle: nil)
    XCTAssertNotNil(sb, "Could not instantiate storyboard for Info View content loading")
    guard let vc = sb.instantiateViewController(withIdentifier: "InformationView") as? InfoViewController else {
        XCTAssert(false, "Could not instantiate view controller for Info view content loading")
        return
    }
    // creates the view controller and sets up the outlets
    _ = vc.view
    guard let txt = vc.txtInfo.text else {
        XCTAssert(false, "Could not get initial text content for Info View content loading")
        return
    }
    
    // need to wait for loadContent()
    vc.loadContent()
    let pred = NSPredicate(format: "text != %@", txt)
    // a condition to be fulfilled
    let exp = expectation(for: pred, evaluatedWith: vc.txtInfo, handler: nil)
    // wait five seconds
    let result = XCTWaiter.wait(for: [exp], timeout: 5.0)
    XCTAssert(result == XCTWaiter.Result.completed, "Loading content for Info View did not change text")
}

I am doing this tutorial in July of 2019. Is the server still up? Could updates to Swift since the video was made be the problem? Do I have a code error that I’m not noticing?

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

1 Like

Hi guys,
Thanks for the great content.

But in this video has an error and also my code it isn’t going green.

func testInfoLoading() {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    XCTAssertNotNil(storyboard, "Could not instantiate storyboard for Info View content loading")
    guard let vc = storyboard.instantiateViewController(withIdentifier: "InformationView") as? InfoViewController else {
        XCTAssert(false, "Could not instantiate for Info View content loading")
        return
    }
    _ = vc.view
    
    guard let txt = vc.txtInfo.text else {
        XCTAssert(false, "Could not get initial text content for Info View content loading")
        return
    }
   
    vc.loadContent()
    
    let predicate = NSPredicate(format: "text != %@", txt)
    let exp = expectation(for: predicate, evaluatedWith: vc.txtInfo, handler: nil)
    let result = XCTWaiter.wait(for: [exp], timeout: 5.0)
    XCTAssert(result != XCTWaiter.Result.completed, "Loading content for Info View did not change text")
}

Thanks

@bdmoakley Do you have any feedback about this? Thank you - much appreciated! :]