Context¶
class Context(val context: CoroutineContext) : TestConfig.Element
Configures the CoroutineContext of the started tests.
When a test is executed, the configured context is injected. Note that the Prepared library injects some context elements itself, which will thus be ignored even if set using this configuration.
-
The
CoroutineNamewill be set to the test's name. -
The
CoroutineDispatchermust not be changed, or it will break thevirtual time.
Because of this, this configuration is likely only useful to inject your own coroutine context elements, not any of the built-ins.
If multiple Context instances are placed in the same test config, the behavior is the same as when combining multiple CoroutineContext elements: later elements of a given type override previous elements of the same type.
Example¶
Let's imagine that you have an AuthContext element that stores the current user, and is used throughout your codebase to check access rights of the current user. You can use this configuration to inject it into all tests that are part of suite:
Or within a specific test:
See also¶
coroutineContext: Access the configured elements.
Constructors¶
Context¶
constructor(context: CoroutineContext)
Types¶
Companion¶
object Companion : TestConfig.Key.Multi<Context>
Properties¶
context¶
val context: CoroutineContext
coroutineContext¶
The coroutine context configured for this test through the Context configuration.
Note that this is different from the coroutine context actually used to run tests; the value returned by this method contains the context declared in the config, whereas more elements are injected by the test machinery when the test actually starts.
key¶
open override val key: Context.Companion
The identifier for this test configuration element.
Functions¶
get¶
operator fun <E : TestConfig.Element> TestConfig.get(key: TestConfig.Key<E, TestConfig.Uniqueness.Unique>): E?
Finds the Element identified by key in the current TestConfig.
It's not possible for multiple elements to share a key marked with Uniqueness.Unique, so this function can never return multiple results. However, no elements may be identified by a key, in which case null is returned.
operator fun <E : TestConfig.Element> TestConfig.get(key: TestConfig.Key<E, TestConfig.Uniqueness.Multi>): List<E>
Finds the elements identified by key in the current TestConfig.
Keys marked with Uniqueness.Multi allow multiple elements of the same type, so this function returns a list. If no elements are found, the returned list is empty.
plus¶
operator fun TestConfig.plus(other: TestConfig): TestConfig
Combines two TestConfig instances.
The elements of other always override or combine with the elements with the same key from the receiver (depending on the Key.uniqueness).