0

我有一个登录视图控制器,用户 Almofire 库来获取响应。我在该控制器上进行单元测试,但测试总是失败。我想是因为需要时间来回应。

我的测试用例:

override func setUp() {

    super.setUp()
    continueAfterFailure = false
    let vc = UIStoryboard(name: "Main", bundle: nil)
    controllerUnderTest = vc.instantiateViewController(withIdentifier: "LoginVC") as! LoginViewController
    controllerUnderTest.loadView()

}

override func tearDown() {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    controllerUnderTest = nil
    super.tearDown()
}

func testLoginWithValidUserInfo() {
    controllerUnderTest.email?.text = "raghad"
    controllerUnderTest.pass?.text = "1234"
    controllerUnderTest.loginButton?.sendActions(for: .touchUpInside)
    XCTAssertEqual(controllerUnderTest.lblValidationMessage?.text , "logged in successfully")
}

我尝试使用:

waitForExpectations(超时:60,处理程序:无)

但我得到了这个错误:

捕获“NSInternalInconsistencyException”

登录演示者中的 almofire 功能:

    func sendRequest(withParameters parameters: [String : String]) {
    Alamofire.request(LOGINURL, method: .post, parameters: parameters).validate ().responseJSON { response in
        debugPrint("new line : \(response)" )
        switch response.result {
        case .success(let value):
            let userJSON = JSON(value)
            self.readResponse(data: userJSON)
        case .failure(let error):
            print("Error \(String(describing: error))")
            self.delegate.showMessage("* Connection issue ")

        }
        self.delegate.removeLoadingScreen()
        //firebase log in
        Auth.auth().signIn(withEmail: parameters["email"]!, password: parameters["pass"]!) { [weak self] user, error in
            //guard let strongSelf = self else { return }
            if(user != nil){
                print("login with firebase")

            }
            else{
                print("eroor in somthing")
            }
            if(error != nil){
                print("idon now")
            }
            // ...
        }
    }

}

func readResponse(data: JSON) {
    switch data["error"].stringValue  {
    case "true":
        self.delegate.showMessage("* Invalid user name or password")
    case "false":
        if  data["state"].stringValue=="0" {
            self.delegate.showMessage("logged in successfully")

        }else {
            self.delegate.showMessage("* Inactive account")
        }
    default:

        self.delegate.showMessage("* Connection issue")

    }
}

我怎么解决这个问题?:(

4

2 回答 2

1

@Raghad ak,欢迎来到 Stack Overflow。

您对阻止测试成功的时间流逝的猜测是正确的。

网络代码是异步的。在登录按钮上的测试调用后,.sendActions(for: .touchUpInside)它会移动到下一行,而不会给回调一个运行的机会。

就像@ajeferson的回答所暗示的那样,从长远来看,我建议将您的 Alamofire 调用放在服务类或协议之后,以便您可以在测试中用双精度替换它们。

除非您正在编写集成测试来测试系统在现实世界中的行为,否则访问网络可能弊大于利。这篇文章更详细地说明了为什么会这样。

说了这么多,这里有一个让你的测试通过的快速方法。基本上,您需要找到一种方法让测试等待您的异步代码完成,并且您可以通过改进的异步期望来完成它。

在您的测试中,您可以这样做:

expectation(
  for: NSPredicate(
    block: { input, _ -> Bool in
      guard let label = input as? UILabel else { return false }
        return label.text == "logged in successfully"
      }
    ),
    evaluatedWith: controllerUnderTest.lblValidationMessage,
    handler: .none
)

controllerUnderTest.loginButton?.sendActions(for: .touchUpInside)

waitForExpectations(timeout: 10, handler: nil)

该期望将NSPredicate在循环中运行,并且仅在谓词返回时才实现true

于 2019-04-07T20:48:09.220 回答
0

您必须以某种方式向您的测试发出可以安全进行的信号(即满足预期)。理想的方法是解耦 Alamofire 代码并在测试时模拟其行为。但只是为了回答您的问题,您可能需要执行以下操作。

在您的视图控制器中:

func sendRequest(withParameters parameters: [String : String], completionHandler: (() -> Void)?) {

  ...

  Alamofire.request(LOGINURL, method: .post, parameters: parameters).validate ().responseJSON { response in

    ...

    // Put this wherever appropriate inside the responseJSON closure
    completionHandler?()
  }
}

然后在你的测试中:

func testLoginWithValidUserInfo() {
    controllerUnderTest.email?.text = "raghad"
    controllerUnderTest.pass?.text = "1234"
    controllerUnderTest.loginButton?.sendActions(for: .touchUpInside)
    let expectation = self.expectation(description: "logged in successfully)
    waitForExpectations(timeout: 60, handler: nil)

    controllerUnderTest.sendRequest(withParameters: [:]) {
      expectation.fulfill()
    }

    XCTAssertEqual(controllerUnderTest.lblValidationMessage?.text , "logged in successfully")
}

我知道您在按钮单击和调用函数之间有一些中间函数sendRequest,但这只是为了让您了解。希望能帮助到你!

于 2019-04-07T14:12:59.563 回答