如何使用RxSwift强制点击UIButton?

时间:2018-08-02 11:03:15

标签: ios swift testing rx-swift

这是我在UIButton上订阅轻按动作的方式:

_ = mainView.loginButton.rx.tap.subscribe { _ in
    //this is not called at all
}

现在,我需要强制执行该操作以检查我的依赖项是否运行良好,但是未调用订阅处理程序。

func testRouterDidCallWithError() {
    let view = LoginView()
    let controller = LoginViewController(view: view)
    controller.loadView()
    view.loginButton.sendActions(for: .touchUpInside) //here is the point
}

1 个答案:

答案 0 :(得分:1)

这有效(“ ReactiveX / RxSwift”〜> 4.0)

someButton.rx.tap
        .bind {
            debugPrint("button tapped")
        }
        .disposed(by: disposeBag)

其他地方

someButton.sendActions(for: .touchUpInside)