NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: URL Asynchronous testing, unexpected breakpoint

Forums > Swift

Hey guys. I am learning how to do testing.

I am having a little issue with that code. It throws a breakpoint at XCTAssertEqual() line (I've checked and I ma sure that no breakpoint was set by me). Test won't go any further no matter what I do, and testing will never end.

I have checked response's status code and existence of an error in seperate test. Follower is a Class that conforms to FollowerProtocol. (I am following some examples from Swift Testing book by Paul Hudson). Anyone happens to know what am I doing wrong here? Many thanks.

 func testFetchedFollowersFromGithubEqualTo1() {
        // Assert
        let sut         = URLSession(configuration: .default)
        let url         = URL(string: "https://api.github.com/users/jmgawecki/followers?per_page=1&page=1")

        let expectation = XCTestExpectation(description: "Completion handler invoked")

        var followers:  [Follower] = []
        let decoder     = JSONDecoder()

        // Act
        let dataTask    = sut.dataTask(with: url!) { (data, response, error) in
            guard let data = data else { return }

            do {
                decoder.keyDecodingStrategy = .convertFromSnakeCase
                followers                   = try decoder.decode([Follower].self, from: data)
            } catch {
                XCTFail("Catch block error")
            }

            expectation.fulfill()

            // Assert
            XCTAssertEqual(followers.count, 1)
        }
        dataTask.resume()

        wait(for: [expectation], timeout: 5)
    }

1      

Solved, breakpoints are automatic for those tests if the xtcasssert is not passing.

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.