Rxjava schedulers. Immediate() behavior during unit testing
I'm trying to write tests for Dao objects that use a reactive interface. I have a table containing recipes. I want to test it. When I insert data into the table, subscribers will receive a list containing recipes
I am using the testsubscriber class and executing assertions on it. My simple test is as follows:
@Test
fun testSubscriber() {
insertItem()
val testSubscriber = TestSubscriber.create<List<Recipe>>()
recipeDao
.getRecipes()
.subscribeOn(Schedulers.immediate())
.subscribe(testSubscriber)
testSubscriber.assertNoErrors()
testSubscriber.assertNoTerminalEvent()
testSubscriber.assertNotCompleted()
testSubscriber.assertValueCount(1)
assertEquals(1, testSubscriber.onNextEvents[0].size)
}
The problem is that the assertion of testsubscriber.assertvaluecount (1) failed because no items were issued. However, when I insert this line above, testsubscriber.awaitterminalevent (500, timeunit. Milliseconds), the test was successful. My observable will not issue terminal events, so it will execute timeout, but while waiting, I called onnext using the recipe list
My getrecipes method:
fun getRecipes(): Observable<List<Recipe>> {
return query(SELECT("*")
.FROM(Recipe.TABLE_NAME)
.ORDER_BY(Recipe.COL_NAME))
.run()
.mapToList(RecipeMapper.MAPPER)
}
How is that possible? I think that when I use schedulers. Immediate(), the operation will be executed on the same thread and my testsubscriber will receive events. If not, how should I write this test to make it successful? I want to test whether onnext is called, and I don't want to insert an artificial sleep command between the two
resolvent:
The problem is that I am using the library sqlbrite with the additional framework sqlbrite Dao. Sqlbrite is observing queries on a specific scheduler and will use schedulers. Io() when no queries are provided to the daomanager of sqlbrite Dao. The solution is to provide a scheduler to daomanager. Builder, or apply rxjavaplugins and return schedulers. Immediate() as all schedulers