How to use MockK to mock an observable

1.8k Views Asked by At

I have a data provider that has an Observable<Int> as part of the public API. My class under test maps this into a Observable<String>.

How do I create a mock so that it can send out different values on the data provider's observable?

I can do it using a Fake object, but that is a lot of work that I don't think is necessary with MockK.

Simplified code:

interface DataProvider {
    val numberData:Observable<Int>
}

class FakeDataProvider():DataProvider {

    private  val _numberData = BehaviorSubject.createDefault(0)
    override val  numberData = _numberData.hide()

    // Note: the internals of this class cause the _numberData changes.
    // I can use this method to fake the changes for this fake object,
    // but the real class doesn't have this method.

    fun fakeNewNumber( newNumber:Int ) {
        _numberData.onNext( newNumber )
    }
}

interface ClassUnderTest {
    val stringData:Observable<String>

}
class MyClassUnderTest( dataProvider: DataProvider ):ClassUnderTest {

    override val stringData = dataProvider.numberData.map { "string = " + it.toString() }
}

class MockKTests {

    @Test fun testUsingFakeDataProvider() {

        val fakeDataProvider        = FakeDataProvider()
        val classUnderTest          = MyClassUnderTest( fakeDataProvider )

        val stringDataTestObserver  = TestObserver<String>()

        classUnderTest.stringData.subscribe( stringDataTestObserver )

        fakeDataProvider.fakeNewNumber( 1 )
        fakeDataProvider.fakeNewNumber( 2 )
        fakeDataProvider.fakeNewNumber( 3 )

        // Note we are expecting the initial value of 0 to also come through
        stringDataTestObserver.assertValuesOnly( "string = 0", "string = 1","string = 2","string = 3" )
    }

    // How do you write the mock to trigger the dataProvider observable?
    @Test fun testUsingMockDataProvider() {
        val mockDataProvider        = mockk<DataProvider>()

//        every { ... what goes here ... } just Runs

        val classUnderTest          = MyClassUnderTest( mockDataProvider )

        val stringDataTestObserver  = TestObserver<String>()

        classUnderTest.stringData.subscribe( stringDataTestObserver )


        // Note we are expecting the initial value of 0 to also come through
        stringDataTestObserver.assertValuesOnly( "string = 0", "string = 1","string = 2","string = 3" )
    }
}
2

There are 2 best solutions below

0
On

Try to use following:

every { mockDataProvider.numberData } answers { Observable.range(1, 3) }

And maybe you need to use another way to make a mock object, like this:

val mockDataProvider = spyk(DataProvider())

0
On

Do something like this where we create an observable fakelist of the observable

var fakeList :List<Quiz> = (listOf<Quiz>(
        Quiz("G1","fromtest","","",1)
    ))
    var observableFakelist = Observable.fromArray(fakeList)

you can then return your observableFakelist.