ScopeFactory

@ExperimentalCoroutinesApi
open class ScopeFactory

Class used to create the TestProvidedCoroutineScope used in CoroutineTestExtension.

In order to provide a custom implementation of TestProvidedCoroutineScope:

  1. Create a custom factory which has a default constructor and extends this ScopeFactory

  2. Annotate your test class with CoroutineTest and pass your custom factory's KClass in as its parameter.

Samples

import dispatch.test.CoroutineTest
import dispatch.test.CoroutineTestExtension
import dispatch.test.TestProvidedCoroutineScope
import io.kotest.matchers.shouldNotBe
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
fun main() { 
   //sampleStart 
   class CoroutineTestNamedFactorySample {

  class TestCoroutineScopeWithJobFactory : CoroutineTestExtension.ScopeFactory() {

    override fun create(): TestProvidedCoroutineScope {
      return TestProvidedCoroutineScope(context = Job())
    }
  }

  @CoroutineTest(TestCoroutineScopeWithJobFactory::class)
  class CustomFactorySample(val testScope: TestProvidedCoroutineScope) {

    @Test
    fun `injected scope should have a Job context`() = runBlocking {

      testScope.coroutineContext[Job] shouldNotBe null
    }
  }
} 
   //sampleEnd
}

Constructors

Link copied to clipboard
fun ScopeFactory()

Functions

Link copied to clipboard
open fun create(): TestProvidedCoroutineScope

Creates an instance of TestProvidedCoroutineScope. Uses the no-arg factory by default.

Sources

Link copied to clipboard