[英]How do you write unit tests with Parse in Xcode (Swift in my case)?
這是我的查詢。 它位於測試目標內。 我在 AppDelegate.swift 中設置了 applicationID 和 clientKey。
我在 AppDelegate.swift 中放置了一個斷點,所以當我運行測試時肯定會被擊中。 它還擊中了我在下面代碼第 5 行之前設置的斷點(customer.saveinbackgroundWithBlock...)。 但是當我在該行之后放置一個斷點時,它不會命中它,並且測試會“成功”。 此外,解析儀表板顯示未添加任何客戶。
我在普通應用程序目標中測試了相同的查詢,並且奏效了。 只是不在測試目標中。
func testCustomersExistCase() {
// Save a test customer
let customer = Customer(firstName: "Jonathan", lastName: "Goldsmith", email: "theMostInterestingManInTheWorld@en.wikipedia.org", 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.")
}
})
}
}
發生這種情況是因為 parse 塊在與您的測試運行不同的線程上執行,因此您的測試完成時不知道 parse 塊的作用。
要測試異步操作,您應該在 XCTest 子類中創建一個期望並調用 waitForExpectation 方法。 這是一個快速示例:
帶方法的視圖控制器
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了解更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.