3

So I have the following test that keeps failing with the following error:

java.lang.AssertionError: No values (latch = 1, values = 0, errors = 0, completions = 0)

val ocrProcessor = mockk<FirebaseFormProcessor>()
        val date = listOf(DateTextExtraction())

        every { ocrProcessor.scan(any(), any(), any()) } answers {
            thirdArg<OcrResultCallback>().invoke(date)
        }

        viewModel = FormViewModel(ocrProcessor)

        viewModel.addImage(bitmap)

        viewModel.ocrAlert
            .test()
            .assertValue {
                it == date
            }
            .addTo(disposeBag)

What this tries to test is the following:

override val ocrAlert: PublishSubject<List<TextExtractionInterface>> = PublishSubject.create()

override fun addImage(bitmap: Bitmap) {
    if (files.value.isEmpty())
        ocrProcessor.scan(bitmap, extract = textExtractionItems) { ocrResult ->
            ocrAlert.onNext(ocrResult)
        }
}

I am not quite sure what I am doing wrong here but I think it might have to do with threading problems.

edit:

I changed the code to this now:

        val toBeTested = viewModel.ocrAlert
            .subscribeOn(scheduler)
            .observeOn(scheduler)
            .test()

        viewModel.addImage(bitmap)

         toBeTested   
            .assertValue {
                it == date
            }
            .addTo(disposeBag)
Reshad
  • 2,570
  • 8
  • 45
  • 86
  • 1
    `orcAlert` doesn't have any item the moment you test it thus the assertin fails. You have to call `test()`, signal `orcalert.onNext`, then call `TestObserver::assertValue` to see the item has arrived. – akarnokd Jan 17 '20 at 08:39
  • @akarnokd thanks for the response. So I have to break up the code? I already do addImage() before anything else which should trigger the onNext if I am correct. – Reshad Jan 17 '20 at 11:30
  • 1
    `PublishSubject` doesn't store items so if there is nothing there to observe it, it will appear empty again. – akarnokd Jan 17 '20 at 12:14
  • how can I modify my test, in such a way, that it tests the correct way? An example would be great :) @akarnokd – Reshad Jan 17 '20 at 12:18
  • Split the code as I suggested. – akarnokd Jan 17 '20 at 12:23
  • That didn't make any difference unfortunately. Or maybe I am doing it wrong. @akarnokd – Reshad Jan 17 '20 at 14:34
  • Please post your entire new test. – akarnokd Jan 20 '20 at 09:03
  • @akarnokd I added how I think you explained it but not sure if that is correct :) Could you post the correct version of what you mean? – Reshad Jan 20 '20 at 11:10
  • What is `scheduler` and where does it come from? – akarnokd Jan 20 '20 at 11:51
  • scheduler is a simple TestScheduler() which I do scheduler.triggerAction on as soon as the callback gets triggered. – Reshad Jan 20 '20 at 12:53
  • Call `scheduler.triggerActions()` before and after the call `viewModel.addImage(bitmap)`. – akarnokd Jan 20 '20 at 12:56
  • that too didn't work unfortunately. – Reshad Jan 20 '20 at 20:23
  • was thinking some throttle, buffer operator maybe could do the trick but @akarnokd wrote the library. – trocchietto Jan 20 '20 at 20:30
  • You are doing or have something wrong not shown. Please post an entire project demonstrating your issue as code snippets isn't enough to diagnose the problem. – akarnokd Jan 21 '20 at 08:28
  • @akarnokd I created a project demonstrating the issue here: https://github.com/reshadf/rxjava-tests – Reshad Jan 21 '20 at 12:53
  • 1
    You haven't even tried to change your code like I suggested! If you would have, the test were passing. https://imgur.com/a/zcYXz3A – akarnokd Jan 22 '20 at 09:29
  • @akarnokd I think you should send it like an answer. I'll put a like there. It's helped me – Ilya Mashin Jul 20 '22 at 07:42

0 Answers0