简体   繁体   English

如何在Alamofire响应上XCTAssertEqual?

[英]How to XCTAssertEqual on Alamofire response?

I am trying to make unit testing for my API with Alamofire as the rest framework. 我正在尝试使用Alamofire作为其余框架对我的API进行单元测试。 I have added the pod dependencies and everything in the Podfile and there is no error regarding missing modules or something. 我在Podfile中添加了pod依赖项和所有内容,关于丢失模块或其他内容没有错误。 Currently as an example I am trying to hit google homepage and on response I am trying to evaluate the response code with XCRAssertEqual . 目前作为示例,我试图访问google主页,并在响应时尝试使用XCRAssertEqual评估响应代码。 The function is working fine if I use in view controller but it's not working in test class. 如果我在视图控制器中使用该函数,则该函数运行良好,但在测试类中却无法运行。 By not working I meant it's giving true for both cases, for both response code being equal to .success and .filure . 通过不工作,我的意思是这两种情况都是正确的,因为两个响应代码都等于.success.filure What can be the reason of this? 这可能是什么原因? Below are my TestClass where function is defined and the test case class where it is being used 以下是我的TestClass,其中定义了函数以及正在使用该函数的测试用例类

import Foundation
import Alamofire

class TestingClass {

    private init(){

    }

    static let sharedInstance = TestingClass()

    func getSquare(number:Int)->Int{
        return number * number
    }

    func getGoogleResponse(completion:@escaping (_ rest:Int)->Void){

        Alamofire.request("https://google.com").responseString { response in
            var result = -1
            switch response.result {
            case .success:
                result = 0
            case .failure(let error):
                result = 1
            }
            completion(result)
        }

    }

}

test case class 测试用例类

import XCTest
@testable import MyApp

class MyAppTests: XCTestCase {

    func testSquare(){
        XCTAssertEqual(TestingClass.sharedInstance.getSquare(number:2),6)
    }

    func testGoogle(){
        TestingClass.sharedInstance.getGoogleResponse { (res) in
            print("ANURAN \(res)")
            XCTAssertEqual(res, 0)
        }
    }
}

First test case is working fine as it has nothing to do with Alamofire but second time never fails. 第一个测试用例工作正常,因为它与Alamofire无关,但是第二个永远都不会失败。

Though I know Alamofire requests are asynchronous it did not hit my mind that it could fail my test case aslo. 尽管我知道Alamofire请求是异步的,但我并没有想到它也可能无法通过我的测试用例。 So what you should do is wait for the response. 因此,您应该做的就是等待响应。 To do that you need to use expectation which comes with XCTestCase . 为此,您需要使用XCTestCase随附的期望 So rewritten code will be like this: 因此,重写的代码将如下所示:

import XCTest
@testable import MyApp

class MyAppTests: XCTestCase {

    func testSquare(){
        XCTAssertEqual(TestingClass.sharedInstance.getSquare(number:2),6)
    }

    func testGoogle(){
        let expectation = self.expectation(description: "Hitting Google")
        var result:Int?
        TestingClass.sharedInstance.getGoogleResponse { (res) in
            print("ANURAN \(res)")
            result=res
            expectation.fulfill()
        }
        wait(for: [expectation], timeout: 30)
        XCTAssertEqual(result!, 1)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM