2016-11-23 3 views
1

データベースにデータを追加して取得する簡単なアプリケーションを作成しました。単体テストの作成中はURLSession.Shared.dataTaskが実行されていないようです。私はセットアップした印刷ステートメントの出力を通してこれを見ることができます。以下は私のコードです:ユニットテスト時にXcode:URL共有セッションが実行されない

func addChild(childName:String,dob:String,number1:String,number2:String,parentNum:String,parentPass:String,notes:String){ 

    //url to php file 
    let url = NSURL(string:"http://localhost/addChild.php") 

    //request to this file 
    let request = NSMutableURLRequest(url: url as! URL) 

    //method to pass data to this file 
    request.httpMethod = "POST" 

    //body to be appended to url 
    let body = "childName=\(childName)&dateOfBirth=\(dob)&contact1=\(number1)&contact2=\(number2)&parentAccNum=\(parentNum)&parentAccPass=\(parentPass)&notes=\(notes)" 
    request.httpBody = body.data(using: String.Encoding.utf8) 
    print("a") 
    //launching the request 
    URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) in 
     print("b") 
     if (error == nil){ 
      print("c") 
      //send request 
      //get main queue in code process to communicate back to user interface 
      DispatchQueue.main.async(execute: { 

       do{ 
        //get json result 
        let json = try JSONSerialization.jsonObject(with: data!,options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary 
        print("d") 
        //assigning json to parseJSON in guard/secure way 
        //checking whether the parsing has worked 
        guard let parseJSON = json else{ 
         print("Error while parsing") 
         return 
        } 

        //get id from parseJSON dictionary 
        let id = parseJSON["id"] 

        //if there is some id value 
        if id != nil{ 
         print(parseJSON) 
         self.success = true 
         print("success") 
        } 
       } 
       catch{ 
        print("Caught an error:\(error)") 
       } 


      }) 
     } 
      //if unable to proceed request 
     else{ 
      print("Error:\(error)") 
     } 
     //launch prepared session 
    }).resume() 
} 

そして以下は、私のユニットテストスクリプトです:ここ

import XCTest 
@testable import computerScienceCoursework 


class addChildTest: XCTestCase { 
    //Setting up the values of the text fields 
    var testChildName:String = "Test name" 
    var testDOB:String = "99/99/99" 
    var testContact1:String = "00000000000" 
    var testContact2:String = "11111111111" 
    var testParAccNum:String = "-1" 
    var testParAccPass:String = "Password" 
    var testNotes:String = "Insert notes here" 



    var newChild = AddChildController() 

    override func setUp() { 
     super.setUp() 
     // Put setup code here. This method is called before the invocation of each test method in the class. 


     } 

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

    func testAddChildIsWorking(){ 
     //Assigning the values to the text fields 
     newChild.addChild(childName: testChildName,dob: testDOB,number1: testContact1,number2: testContact2,parentNum: testParAccNum,parentPass: testParAccPass,notes: testNotes) 
     XCTAssert(newChild.success == true) 
    } 

} 

答えて

0

問題は、非同期タスクが完了すると、成功のプロパティが更新取得されたときに、あなたが知っているドントということです。 問題には、メソッドに補完ハンドラを追加する方法があります。

func addChild(childName:String,dob:String,number1:String,number2:String,parentNum:String,parentPass:String,notes:String, completion: (Bool) -> Void){ 
    //url to php file 
    let url = NSURL(string:"http://localhost/addChild.php") 

    //request to this file 
    let request = NSMutableURLRequest(url: url as! URL) 

    //method to pass data to this file 
    request.httpMethod = "POST" 

    //body to be appended to url 
    let body = "childName=\(childName)&dateOfBirth=\(dob)&contact1=\(number1)&contact2=\(number2)&parentAccNum=\(parentNum)&parentAccPass=\(parentPass)&notes=\(notes)" 
    request.httpBody = body.data(using: String.Encoding.utf8) 
    print("a") 
    //launching the request 
    URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) in 
     print("b") 
     if (error == nil){ 
      print("c") 
      //send request 
      //get main queue in code process to communicate back to user interface 
      DispatchQueue.main.async(execute: { 

       do{ 
        //get json result 
        let json = try JSONSerialization.jsonObject(with: data!,options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary 
        print("d") 
        //assigning json to parseJSON in guard/secure way 
        //checking whether the parsing has worked 
        guard let parseJSON = json else{ 
         print("Error while parsing") 
         completion(false) 
         return 
        } 

        //get id from parseJSON dictionary 
        let id = parseJSON["id"] 

        //if there is some id value 
        if id != nil{ 
         print(parseJSON) 
         self.success = true 
         print("success") 
         completion(true) 
        } 
       } 
       catch{ 
        print("Caught an error:\(error)") 
        completion(false) 
       } 

      }) 
     } 
      //if unable to proceed request 
     else{ 
      print("Error:\(error)") 
      completion(false) 

     } 
     //launch prepared session 
    }).resume() 
} 

次に、メソッドをテストすることができます。

 func testAddChildIsWorking() 
     { 
      let asyncExpectation = expectationWithDescription("addChildIsWorkingFunction") 

      newChild.addChild(childName: testChildName, dob: testDOB, number1: testContact1, 
      number2: testContact2, parentNum: testParAccNum, parentPass: testParAccPass, notes: testNotes) { (success) in 

       asyncExpectation.fulfill() 

      } 

      self.waitForExpectationsWithTimeout(10) { error in 

       XCTAssert(newChild.success == true) 
      }  
     } 

waitForExpectationWithTimeoutは、達成がトリガーになるかタイムアウトが発生するまで待機しています。このようにして、非同期コードをテストすることができます。 詳細についてはこちらをご確認くださいlink

+0

本当にありがとう、私は正直言って、私は返信を得るとは思わなかった:) –

+0

あなたは大歓迎です。答えがあなたの問題を解決するなら、それらを受け入れてください。 –

+0

実際には少し問題がありますが、newChild.addChild呼び出しでエラーが発生しています: "余分な引数 'Notes' in call"。何があったのか分かりますか? –

関連する問題