Start a Project

Unit Testing in iOS using swift (Part 2)

Network Call Check

In the previous part, we have already know how to set up the unit testing in Xcode project & some basic functionality.

Refer: https://mobikul.com/unit-testing-in-ios-using-swift-part-1/

In this blog, we will discuss how to test the Network Call (Asynchronous function).

In-Network call we have to wait until the data will come to its totally Asynchronous function so that we have to wait in method until the result will come for that swift provide  “expectation”

With the help of this, we can handle Network call.

Example:

Here are Some Steps you need to follow this

1: Set up Class.

here we have taken the example through  “URLSession”

2: Now Start writing the network class checking method.

Valid URL: https://itunes.apple.com/search?media=music&entity=song&term=abba

here we are checking the URL response is valid or not.

Here we have taken the “expectation”  class to hold the network call response.

We have taken two Test checking method

1: XCTAssertNil(responseError,”Any String Value”) .  if responseError returns nil then there is no issue.

2: XCTAssertEqual(statusCode, 200)   if  statusCode == 200 then no issue

if any matches fail then you will get the test fail result.

For checking the error case you just change the URL’s string or Invalid URL & check.

 

This Is The Introduction Of  The Async Network Call & Some Other Tips To Test Valid URL , In next Blog we will talk about the Network request through Alamofire .

Exit mobile version