3

How can I mock the following function act.

class Some {
    internal lateinit var coroutineScope: LifecycleCoroutineScope

    ...

    fun act() {
        coroutineScope.launch {
            ...
        }
    }
}

I am using mockk and kotlinx-coroutines-test for unit-testing this. But it can only mock CoroutineScope and not LifecycleCoroutineScope.

Ashwin M K
  • 91
  • 6
  • 1
    do you need a `LifecycleCoroutineScope` in your code iof you just want to call `launch()`? Probalby a good old `CoroutineScope` would do the job. – Jens Baitinger Apr 12 '21 at 17:26
  • @Jens Baitinger you are right, if I change it to `CoroutineScope`, the test works. I just wanted to check if I can get `TestLifecycleCoroutineScope` somehow so that I don't have to change the code. – Ashwin M K Apr 13 '21 at 01:09
  • If you dont need the more sophisticated interface, you should not have a dependency to it. make your code easy to to test, that usually makes it easier to understand. By having a "only" a `CoroutineScope` you tell the reader of your code that in all around your code you will only call the limited functionality of that interface. when you have your dependency to `TestLifecycleCoroutineScope`, the reader of the code needs to thinl about what is special about that interface, why do we need it here, and that makes the code hard to understand – Jens Baitinger Apr 13 '21 at 20:11

0 Answers0