3

I have a ViewModel which has as an input in its initializer

init(sliderEvents: Reactive<UISlider>) {

In the test i want to do something like

slider.send(.touchDownInside)
slider.send(.valueChanged, 5)
slider.send(.valueChanged, 15)

To simulate for the VM that the slider was dragged from value 5 to 15 for example

It is unclear to me how RAC has built up the Base: UISlider so i'm confused as to how to make the subclass of UISlider to make this kind of mocking possible

bogen
  • 9,954
  • 9
  • 50
  • 89

2 Answers2

3

You could set up the ViewModel to have an Observer, Action or MutableProperty (anything takes inputs) with Double type. Then bind the UISlider values to that in your ViewController.

So in your ViewController you could have a line like this: viewModel.sliderValue <~ slider.reactive.mapControlEvents(.valueChanged){ $0.value } where sliderValue could be of type MutableProperty<Double>.

In your tests you could then set the values like this: viewModelToTest.sliderValue.value = 10

  • You are technically correct, but this would be too much work to change in my given code base. So still looking to see if it is possible to just do a mocked subclass. – bogen Apr 26 '18 at 08:50
2

Here's another approach that should work:

protocol SliderProtocol {
    var valuesSignal: Signal<Float, NoError> { get }
}

extension UISlider: SliderProtocol {
    var valuesSignal: Signal<Float, NoError> {
        return reactive.values
    }
}

extension Reactive where Base: SliderProtocol {
    var values: Signal<Float, NoError> {
        return base.valuesSignal
    }
}

class MockSlider: SliderProtocol {
    let mockValue = MutableProperty<Float>(0)

    var valuesSignal: Signal<Float, NoError> {
        return mockValue.signal
    }
}

Your ViewModel should then init with Reactive<SliderProtocol> and you could pass the MockSlider instance to it. Mocking would be as simple as setting mockSlider.mockValue.value = 10.

  • Hard to get this to work with the rest of methods that require the slider to be a subclass of UIControl, and the ordinary UISlider in my viewcontroller does not comform to SliderProtocol since its just a UISlider – bogen Apr 26 '18 at 11:42