Updated 30 August 2019
In the previous blog, we have covered how to test the URL response is Valid or not using URLSession,
In this blog, we will cover through Almofire.
Previous Blog Link:
In This Blog we are not discussing about the expectaion for handling the Async call, this is already covered in the previous blog.
First,we need to write the common class for network call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
class NetworkManager: NSObject { class var sharedInstance: NetworkManager { struct Singleton { static let instance = NetworkManager() } return Singleton.instance } func callingHttpRequest(params: [String: Any], method: HTTPMethod, apiname: WhichApiCall, taskCallback: @escaping (Int, AnyObject?) -> Void) { let defaults = UserDefaults.standard var urlString: String = "" urlString = hostName + apiname.rawValue var params = params if method == .get { urlString += "?storeId=" + Defaults.storeId urlString += ""eId=" + Defaults.quoteId urlString += "¤cy=" + (Defaults.currency ?? "") urlString += "&customerToken=" + (Defaults.customerToken ?? "") urlString += "&websiteId=" + UrlParams.defaultWebsiteId urlString += "&width=" + UrlParams.width } else { params["currency"] = Defaults.currency } print("auth key", defaults.object(forKey: "authKey") as? String ?? "") print("url", urlString) print("params", params) var headers: HTTPHeaders = [:] if defaults.object(forKey: "authKey") == nil { headers = [ "authKey": "" ] } else { headers = [ "authKey": defaults.object(forKey: "authKey") as? String ?? "" ] } Alamofire.request(urlString, method: method, parameters: params, headers: headers).validate().responseJSON { response in switch response.result { case .success(let resultData): taskCallback(1, resultData as AnyObject) case .failure(let error): taskCallback(0,datastring as AnyObject) } } } } |
Here we have written the network class where it will accept the
1: Api Name. : it the name which appends with your hostName.
2: Request TypeÂ
3: Params
3: hostName: is baseURL like http://www.google.com
First,declare all the variable in the class.
1 2 3 4 5 6 7 8 9 10 11 |
import XCTest @testable import Mobikul_Single_App class CartTestClass: XCTestCase { var requstParams = [String: Any]() var promise:XCTestExpectation! var serverResponseValue:Int = 0 var serverResponseData:AnyObject? } |
1 2 3 4 5 |
override func setUp() { requstParams["storeId"] = Defaults.storeId requstParams["customerToken"] = Defaults.customerToken requstParams["quoteId"] = Defaults.quoteId } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
func testCartAPIRequest() { promise = expectation(description: "CallBack handler invoked") NetworkManager.sharedInstance.callingHttpRequest(params: requstParams, method: .get, apiname: .cartData,showPopup:false) { [weak self] (value,responseMessage) in self?.serverResponseValue = value self?.serverResponseData = responseMessage self?.promise.fulfill() } wait(for: [self.promise], timeout: 10) switch serverResponseValue { case 1: let data = JSON(serverResponseData as? NSDictionary ?? [:]) XCTAssertTrue(true) case 2: XCTFail("errorTestMessage \(data)") } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import XCTest @testable import Mobikul_Single_App class CartTestClass: XCTestCase { var requstParams = [String: Any]() var promise:XCTestExpectation! var serverResponseValue:Int = 0 var serverResponseData:AnyObject? override func setUp() { requstParams["storeId"] = Defaults.storeId requstParams["customerToken"] = Defaults.customerToken requstParams["quoteId"] = Defaults.quoteId } override func tearDown() { cartModel = nil super.tearDown() } func testCartAPIRequest() { promise = expectation(description: "CallBack handler invoked") NetworkManager.sharedInstance.callingHttpRequest(params: requstParams, method: .get, apiname: .cartData,showPopup:false) { [weak self] (value,responseMessage) in self?.serverResponseValue = value self?.serverResponseData = responseMessage self?.promise.fulfill() } wait(for: [self.promise], timeout: 10) switch serverResponseValue { case 1: let data = JSON(serverResponseData as? NSDictionary ?? [:]) XCTAssertTrue(true) case 2: XCTFail("errorTestMessage \(data)") } } } |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.