TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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

3      

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

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.