2016-04-02 9 views
-1

これは私の質問です。これはテスト対象の内部にあります。私はAppDelegate.swiftの中にapplicationIDとclientKeyを設定しました。XcodeでParseで単体テストを書く方法(私の場合はSwift)?

私はAppDelegate.swiftにブレークポイントを設定しています。そのため、テストを実行すると明らかにヒットします。また、以下のコードの5行目の前に設定したブレークポイント(customer.saveinbackgroundWithBlock ...)にヒットしました。しかし、私がその行の直後にブレークポイントを置くと、ヒットしなくなり、テストは成功するでしょう。また、解析ダッシュボードには顧客が追加されていないことが示されています。

通常のアプリターゲットで同じクエリをテストしたところ、うまくいきました。テスト対象ではない。解析ブロックは、あなたのテストよりも別のスレッドで実行されるため、これが起こっている

func testCustomersExistCase() { 
    // Save a test customer 
    let customer = Customer(firstName: "Jonathan", lastName: "Goldsmith", email: "[email protected]", streetAddress: "100 Main St", city: "Monterrey", state: "Texas", zipCode: "55555") 
    customer.shopID = "dosequis" 

    customer.saveInBackgroundWithBlock({ 
     (success: Bool, error: NSError?) -> Void in 
      if success { 
       print("This test should work.") 
       self.customerSearchViewControllerDataSource.getAllCustomers(self.customeSearchViewController) 

       // Check if it's in the customers array 
       for customerResult in self.customeSearchViewController.data! { 
        if customerResult.objectId == customer.objectId { 
         XCTAssertTrue(true, "Success! Customer was added.") 

         // Delete customer if test had succeeded. 
         customer.deleteInBackgroundWithBlock({ 
          (success: Bool, error: NSError?) -> Void in 
          if success { 
           print("Clean-up after test was successful") 
          } else { 
           print("Need to delete customer manually") 
          } 
         }) 
        } else { 
         XCTAssertTrue(false, "Query is broken. Customer was not retrieved") 
        } 
       } 
      } else { 
       print("This test will not work. Customer was not added to Parse") 
      } 
     if error != nil { 
      print("This test isn't working. Parse threw an error.") 
     } 
    }) 
} 

} 

答えて

0

上で実行され、そのテストは、解析ブロックが何をするかを知らない終了。

非同期操作をテストするには、XCTestサブクラスで期待値を作成し、waitForExpectationメソッドを呼び出す必要があります。ここでは簡単な例です:/ w法

ビューコントローラ

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func getUserStuff(completionHandler: (succeeded: Bool) ->()) { 

} 
} 

テスト:

func testGetUserInfo() { 

    let vc = ViewController() 

    let userOpExpectation: XCTestExpectation = expectationWithDescription("Got user info") 

    vc.getUserStuff { (succeeded) in 
     XCTAssert(succeeded, "Should get user stuff") 
     userOpExpectation.fulfill() 
    } 

    waitForExpectationsWithTimeout(5.0, handler: nil) 
} 

テストに入り、Appleが含まの方法の両方で、多くのことを提供たくさんありますコードとドキュメント、チェックアウト:https://developer.apple.com/library/tvos/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/04-writing_tests.html詳細については、

+0

ありがとうございました!私は私の研究で期待を見ましたが、私はそれらを必要とすることは決してありませんでした。 –

+0

@ArthurAyetissよろしくお願いします!あなたは私が確信しているそれらとの流れになるでしょう。 :) –

関連する問題