2016-09-22 39 views
1

讓我開始說明我仍然不熟悉我正在嘗試做什麼,但努力變得更好!單元測試在Swift中使用REST調用的方法

我正在編寫一個項目,我正在編寫單元測試,而且我在如何解決問題方面遇到了一些麻煩。

我正在測試的方法利用RESTAPI調用來驗證用戶憑據。我不確定單元測試的最佳方式是什麼。

下面是我在尋找,以便爲單元測試方法:

@IBAction func loginBtnActivate(sender: UIButton) { 
    let enteredEmail: String = emailField.text! 
    let enteredPassword: String = passwordField.text! 
    let testInfo:[String: AnyObject] = ["User": enteredEmail, "Password": enteredPassword] 
    RestApiManager.sharedInstance.postLogin(testInfo) { (json, statusCode) in 
     if statusCode == 200 { 
      let AuthToken: TokenObject = (TokenObject(json: json)) 
      try! self.keychain.set(AuthToken.Authorization, key:"Authorization") 
      try! self.keychain.set(AuthToken.LifeTime, key: "LifeTime") 
      try! self.keychain.set(AuthToken.UUID, key: "UUID") 
      NSOperationQueue.mainQueue().addOperationWithBlock { 
       self.performSegueWithIdentifier("loginToMyHealth", sender: nil) 
      } 
     } else if statusCode == 401 { 
      self.incorrectLoginAlert() 
     } else if statusCode == 503 { 
      print("Service Unavailable Please Try Again Later") 
     } 
    } 
} 

這是我目前採用的方法:

func testLoginInfoMatchesDataOnServer(){ 
    let enteredEmail: String = "user" 
    let enteredPassword: String = "password" 
    let testInfo:[String: AnyObject] = ["User": enteredEmail, "Password": enteredPassword] 
    RestApiManager.sharedInstance.postLogin(testInfo) { (json, statusCode) in 
    XCTAssert(statusCode == 200, "statusCode is not matching the server data") 
} 

我只是驗證了其餘調用成功,並且憑據與JSON匹配。 XCTAssert調用似乎沒有正常工作。無論我將第一個參數放在哪裏,XCTAssert都不會影響測試是否成功。

例如,如果我把:

XCTAssert(false, "statusCode is not matching the server data") 

測試將仍然通過不管是什麼,我把。如果我將Assert函數置於括號外,那麼它顯示變量「statusCode」超出了範圍,因此我被卡住了

使用未解析標識符'statusCode'。

func testLoginInfoMatchesDataOnServer(){ 
let enteredEmail: String = "user" 
let enteredPassword: String = "password" 
let testInfo:[String: AnyObject] = ["User": enteredEmail, "Password": enteredPassword] 
RestApiManager.sharedInstance.postLogin(testInfo) { (json, statusCode) in 
} 
XCTAssert(statusCode == 200, "statusCode is not matching the server data") 
} 

我一直在尋找在此指南幫助..請問這是什麼,我試圖做一個更好的方法?

http://roadfiresoftware.com/2016/06/how-do-you-unit-test-rest-calls-in-swift/

再次我的一些核心概念的理解可能完全關閉,因此我真的在這裏感謝你的建議!

在此先感謝!

肖恩W.

回答

2

先用你的代碼問題很少

function test(){  
    RestApiManager.sharedInstance.postLogin(testInfo) { (json, statusCode) in 

    }//PostLogin call ends 
    XCTAssert(statusCode == 200, "statusCode is not matching the server data") // Here StatusCode is not accessible as outside the block 
}// end function 

如果你想使用狀態代碼,你應該做的

function test(){  
    RestApiManager.sharedInstance.postLogin(testInfo) { (json, statusCode) in 
     XCTAssert(statusCode == 200, "statusCode is not matching the server data") // Here StatusCode is accessible inside the block 
    }//PostLogin call ends 
}// end function 

但既然你需要等待,這將失敗爲迴應。可以使用

waitForExpectationsWithTimeout(5)

所以正確的方法來調用,這將是完成 - 在這裏

function test(){  
    let URL = NSURL(string: "http://google.com/")! 
    let expectation = expectationWithDescription("GET \(URL)") 
    RestApiManager.sharedInstance.postLogin(testInfo) { (json, statusCode) in 
     XCTAssert(statusCode == 200, "statusCode is not matching the server data") // Here StatusCode is accessible inside the block 
     expectation.fulfill() 
    }//PostLogin call ends 
    waitForExpectationsWithTimeout(5){ error in 
     if let error = error { 
      print("Error: \(error.localizedDescription)") 
     } 
    }//Wait block end 
}// end function 

重要線

預期。履行()//告訴過程完成

waitForExpectationsWithTimeout(5){} //告訴等待5secs或拋出錯誤

更多info

+0

我想這正是我需要的katch!謝謝! –

+0

快速跟進,我應該如何設置我的單元測試?我讀過這是技術上的集成測試,而不是單元測試。我應該使用本地文件而不是依靠對服務器的調用嗎? 我只問你是否碰巧知道。否則,我會繼續做更多的研究。 再次感謝! –

+0

我認爲它是一個單獨的問題。但我會建議離線和在線。離線使用json文件等&在線使用實際電話 – katch

相關問題