2

我已经尝试了所有方法,但获得成功测试的唯一方法是在测试功能中实际发送通知,这有点违背了目的。

我有一个按钮。当我点击按钮时,它会发送通知。如何使用 expectForNotification 来查看是否发送了此通知?

func testExample() {
    let app = XCUIApplication()
    let button = app.buttons["Button"]
    let expectation = expectationForNotification("TEST_NOTE", object: nil) {
        (notification: NSNotification!) -> Bool in

        print("SUCCESS")
        return true
    }

    button.tap()

    waitForExpectationsWithTimeout(5, handler: nil)
}
4

1 回答 1

2

在我看来,您将不得不满足期望...

func testExample() 
{
     let app = XCUIApplication()
     let button = app.buttons["Button"]

     let expectation = expectationWithDescription("waiting for the tap")

      expectationForNotification("TEST_NOTE", object: nil)
      {
        notification in
        expectation.fulfill()
        return true
      }

      button.tap()

      waitForExpectationsWithTimeout(30)
      {
            error in 
            if let e = error
            {
                XCTFail("\(e.debugDescription)")
            }
       } 
}
于 2016-01-15T06:20:07.703 回答