I have a ControlProperty binding like this in my UIViewController:
textView.rx.text.orEmpty.asObservable()
.bind(to: viewModel.descriptions)
.disposed(by: disposeBag)
, where viewModel.descriptions is of type BehaviorRelay<String>.
In a TDD approach, I would like to write an (initially failing) test (as the initial step of the red-green-refactor cycle) to see that this binding exists. I tried with a test code like this:
sut.textView.insertText("DESC")
sut.textView.delegate?.textViewDidChange?(sut.textView)
expect(mockViewModel.lastDescriptions).to(equal("DESC"))
, where sut is my ViewController and mockViewModel contains code to subscribe to the descriptions BehaviorRelay:
class MockViewModel: MyViewModelProtocol {
var descriptions = BehaviorRelay<String>(value: "")
var lastDescriptions: String?
private var disposeBag = DisposeBag()
init() {
descriptions.asObservable()
.skip(1)
.subscribe(onNext: { [weak self] (title: String) in
self?.lastDescriptions = title
})
.disposed(by: disposeBag)
}
}
However, I cannot push the new value to the textView so that the descriptions BehaviorRelay gets it.
How can I achieve descriptions.asObservable() to fire when entering a value in the textView? If it were a UITextField then I would do:
textField.text = "DESC"
textField.sendActions((for: .editingChanged))
Thanks in advance for any help.