json - Unit Testing a method that uses REST calls in Swift -


let me start out stating i'm still unfamiliar i'm trying do, striving better!

i'm working on project i'm writing unit tests , i'm having trouble how approach problem.

the method i'm testing utilizes restapi call verify users credentials. i'm not sure best way unit test be.

here method i'm looking make unit test for:

@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")         }     } } 

this approach i'm taking:

 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 not matching server data") } 

i'm verifying rest call successful , credentials matching json. xctassert call doesn't appear working correctly. no matter put first parameter, xctassert doesn't affect whether test successful or not.

example, if put:

xctassert(false, "statuscode not matching server data") 

the test still pass regardless of put. if place assert function outside brackets appears variable "statuscode" out of scope i'm stuck

use of unresolved identifier '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 not matching server data") } 

i looking @ guide help.. better approach i'm trying do?

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

again understanding of core concepts might entirely off appreciate advice here!

thanks in advance!

sean w.

first few problems code

function test(){         restapimanager.sharedinstance.postlogin(testinfo) { (json, statuscode) in      }//postlogin call ends     xctassert(statuscode == 200, "statuscode not matching server data") // here statuscode not accessible outside block }// end function 

if want use status code should do

function test(){         restapimanager.sharedinstance.postlogin(testinfo) { (json, statuscode) in         xctassert(statuscode == 200, "statuscode not matching server data") // here statuscode accessible inside block     }//postlogin call ends }// end function 

but fail since need wait response. can done using

waitforexpectationswithtimeout(5)

so proper way call --

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 not matching server data") // here statuscode accessible inside block         expectation.fulfill()     }//postlogin call ends     waitforexpectationswithtimeout(5){ error in          if let error = error {             print("error: \(error.localizeddescription)")         }     }//wait block end }// end function 

important lines here

expectation.fulfill() // tells process complete

and

waitforexpectationswithtimeout(5){} //tells wait 5secs or throw error

for more info


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -