coroutineTestExtension

@ExperimentalCoroutinesApi
inline fun coroutineTestExtension(crossinline scopeFactory: () -> TestProvidedCoroutineScope = { TestProvidedCoroutineScope() }): CoroutineTestExtension

Factory function for creating a CoroutineTestExtension.

Samples

import dispatch.test.TestProvidedCoroutineScope
import dispatch.test.coroutineTestExtension
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
fun main() { 
   //sampleStart 
   class RegisterSample {

  @JvmField
  @RegisterExtension
  val extension = coroutineTestExtension()

  @Test
  fun `extension should be a TestProvidedCoroutineScope`() = runBlocking<Unit> {

    extension.scope.shouldBeInstanceOf<TestProvidedCoroutineScope>()
  }

  @Test
  fun `extension should automatically inject into functions`(scope: TestProvidedCoroutineScope) =
    runBlocking {

      val subject = SomeClass(scope)

      val resultDeferred = subject.someFunction()

      scope.advanceUntilIdle()

      resultDeferred.await() shouldBe someValue
    }
} 
   //sampleEnd
}
import dispatch.test.TestProvidedCoroutineScope
import dispatch.test.coroutineTestExtension
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
fun main() { 
   //sampleStart 
   class RegisterWithFactorySample {

  @JvmField
  @RegisterExtension
  val extension = coroutineTestExtension {
    TestProvidedCoroutineScope(context = CoroutineName("custom name"))
  }

  @Test
  fun `extension should provide a scope from the custom factory`() = runBlocking {

    extension.scope.coroutineContext[CoroutineName] shouldBe CoroutineName("custom name")
  }
} 
   //sampleEnd
}

See also

Parameters

scopeFactory

optional factory for a custom TestProvidedCoroutineScope. If a factory is not provided, the resultant scope uses the same TestCoroutineDispatcher for each property in its TestDispatcherProvider

Sources

Link copied to clipboard