Ios UIApp为零,这意味着我们无法向其目标发送控制操作
我正在将文件从一个模块更改为另一个模块,因此我在一个测试中发现了这个错误。而早些时候,它工作得非常好 [Assert]UIApp为零,这意味着我们无法向其目标分派控制操作。如果命中此断言,我们可能在没有执行UIApplicationMain()的情况下到达这里,这可能意味着此代码没有在应用程序中运行(可能是在没有主机应用程序的情况下运行的单元测试),并且将无法按预期工作 在viewDidLoad()中的代码添加按钮中 尝试从测试中点击按钮Ios UIApp为零,这意味着我们无法向其目标发送控制操作,ios,swift,xcode,Ios,Swift,Xcode,我正在将文件从一个模块更改为另一个模块,因此我在一个测试中发现了这个错误。而早些时候,它工作得非常好 [Assert]UIApp为零,这意味着我们无法向其目标分派控制操作。如果命中此断言,我们可能在没有执行UIApplicationMain()的情况下到达这里,这可能意味着此代码没有在应用程序中运行(可能是在没有主机应用程序的情况下运行的单元测试),并且将无法按预期工作 在viewDidLoad()中的代码添加按钮中 尝试从测试中点击按钮 func test() { let vi
func test() {
let viewController = ViewController(viewModel: viewModel)
let button = viewController.view.findViewByIdentifier("paymentButton") as! ABCTypeButton
// I Checked that button is not nil
button.sendActions(for: .touchUpInside)
XCTAssertEqual(viewController.value, button.accessibilityIdentifier)
}
目标方法action1()没有被调用我只是遇到了这个问题,并对touchUpInside事件进行了粗略的扩展。显然可以进行重构,以接收您想要调用的任何事件
extension UIButton {
public func touchUpInside(forTarget target: UIViewController) {
guard let action = actions(forTarget: target, forControlEvent: .touchUpInside)?.first else {
assertionFailure("could not find touchUpInside action for target")
return
}
target.perform(Selector(action))
}
}
你能发布ABCTypeButton类吗。和按钮。sendActions(用于:.touchUpInside)代码。上面的代码不清楚为什么在单元测试中调用
sendActions
?这是一个UI测试。测试sendActions
没有意义,我们知道它的作用。在单元测试中测试您的单元,而不是您的接口行为。如果您想测试点击按钮时发生的情况,请使用UI测试。将代码移动到模块会使测试部分成为框架而不是应用程序,因此UIApp部分实际上将为零,我们不会按任何按钮导致自动调用其目标(这是由应用程序类型的捆绑包完成的,但不是由框架类型的捆绑包完成的)
extension UIButton {
public func touchUpInside(forTarget target: UIViewController) {
guard let action = actions(forTarget: target, forControlEvent: .touchUpInside)?.first else {
assertionFailure("could not find touchUpInside action for target")
return
}
target.perform(Selector(action))
}
}