gpt4 book ai didi

ios - 在单元测试中手动向左滑动以调用处理程序以在 Swift 中进行测试

转载 作者:行者123 更新时间:2023-11-28 05:45:00 25 4
gpt4 key购买 nike

我正在对 tableview 进行单元测试,我需要逻辑滑动吗? UITableView 中的一行。是否可以在单元测试中执行滑动以调用处理程序?我查看了 UITableViewDelegate,但没有滑动操作(didSelectRowAt 在那里并在单元测试中进行了测试)。

func createDeleteHandler(tableView : UITableView, indexPath : IndexPath) -> UIContextualAction.Handler {
let deleteHandler = { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
let noteToBeDeleted = self.notes[indexPath.row]
NoteManager.shared.deleteNote(note: noteToBeDeleted)
self.notes.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
success(true)
}

return deleteHandler
}

最佳答案

您可以像这样使用 XCUITest 测试:

import XCTest

class MoreUITests: XCTestCase {

override func setUp() {
continueAfterFailure = false
XCUIApplication().launch()
}

func testUI() {

let app = XCUIApplication()
let tablesQuery = app.tables
let addButton = app.navigationBars["Master"].buttons["Add"]
let masterButton = app.navigationBars["Detail"].buttons["Master"]
addButton.tap() // adds Item-0
addButton.tap() // adds Item-1
addButton.tap() // adds Item-2
addButton.tap() // adds Item-3

tablesQuery.staticTexts["Item-1"].tap()

// Go back
masterButton.tap()

// Swipe Left on item-2
tablesQuery.staticTexts["Item-2"].swipeLeft()

}
}

最简单的方法是使用 Xcode UI 记录器记录它们。更多细节:

https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/09-ui_testing.html

这是我用滑动记录的示例:

https://youtu.be/lHafMlIcoCY

关于ios - 在单元测试中手动向左滑动以调用处理程序以在 Swift 中进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55129843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com