TransmissionTest

Comprehensive testing framework for Transmission components with a fluent DSL.

TransmissionTest provides a sophisticated testing framework specifically designed for testing Transformer components in isolation or with controlled dependencies. It supports mocking data holders, computations, executions, and checkpoint validation while providing a clean fluent API for test setup and assertions.

Key Features:

  • Transformer Isolation: Test transformers in isolation with mocked dependencies

  • Mock Support: Mock data holders, computations, and executions from other transformers

  • Checkpoint Testing: Support for testing experimental checkpoint-based flow control

  • Stream Capture: Automatic capture of data and effect streams for assertions

  • Coroutine Testing: Built-in integration with kotlinx-coroutines-test

  • Initial Processing: Support for processing transmissions before the main test

  • Type-Safe Assertions: Type-safe methods for asserting on specific data/effect types

Basic Usage:

Testing Signal Processing:

@Test
fun testUserLogin() = runTest {
val transformer = UserTransformer()

transformer.test()
.testSignal(UserSignal.Login("username", "password")) {
// Assert that login data was emitted
val userData = lastData<UserData.LoggedIn>()
assertNotNull(userData)
assertEquals("username", userData.user.name)
}
}

Testing with Mocked Dependencies:

@Test
fun testUserProfile() = runTest {
val transformer = ProfileTransformer()

transformer.test()
.withData(UserRepository.userContract) {
UserData.LoggedIn(User("John", "john@example.com"))
}
.withComputation(ProfileService.profileContract) {
UserProfile("John", 25, "Engineer")
}
.testSignal(ProfileSignal.LoadProfile) {
val profileData = lastData<ProfileData.Loaded>()
assertNotNull(profileData)
assertEquals("John", profileData.profile.name)
}
}

Testing Effect Processing:

@Test
fun testCacheInvalidation() = runTest {
val transformer = CacheTransformer()

transformer.test()
.testEffect(CacheEffect.Invalidate("user_data")) {
// Assert that cache was cleared
val clearEffect = lastEffect<CacheEffect.Cleared>()
assertNotNull(clearEffect)
}
}

Testing with Initial Processing:

@Test
fun testWithInitialState() = runTest {
val transformer = DataTransformer()

transformer.test()
.withInitialProcessing(
UserSignal.Login("user", "pass"),
DataSignal.Initialize
)
.testSignal(DataSignal.Refresh) {
// Test behavior after initial processing
assertTrue(dataStream.size >= 2)
}
}

Parameters

transformer

The transformer to test

dispatcher

The test dispatcher for controlling coroutine execution

See also

for creating test instances

for assertion methods

Types

Link copied to clipboard
object Companion
Link copied to clipboard
class TestResult(val dataStream: List<Transmission.Data>, val effectStream: List<Transmission.Effect>)

Contains the captured streams and provides assertion methods for test validation.

Functions

Link copied to clipboard
fun testEffect(effect: Transmission.Effect, assertions: suspend TransmissionTest.TestResult.() -> Unit)

Executes a test with the given effect transmission and runs the provided assertions.

Link copied to clipboard
fun testSignal(signal: Transmission.Signal, assertions: suspend TransmissionTest.TestResult.() -> Unit)

Executes a test with the given signal transmission and runs the provided assertions.

Link copied to clipboard

Adds a default checkpoint that will be validated during testing.

Adds a checkpoint with arguments that will be validated during testing.

Link copied to clipboard
fun <C : Contract.ComputationWithArgs<A, D?>, D : Any, A : Any> withComputation(contract: C, data: () -> D?): TransmissionTest

Adds a mock computation with arguments that provides computed results for the specified contract during testing.

fun <C : Contract.Computation<D?>, D : Any> withComputation(contract: C, data: () -> D?): TransmissionTest

Adds a mock computation that provides computed results for the specified contract during testing.

Link copied to clipboard

Adds a mock data holder that provides data for the specified contract during testing.

Link copied to clipboard

Sets up initial transmissions that should be processed before the main test transmission.