-
Notifications
You must be signed in to change notification settings - Fork 0
Test Integration
Ali Sadeghi edited this page Jan 6, 2026
·
7 revisions
Generates E2E integration tests (MockEngine → ViewModel).
// feature/login/src/commonTest/kotlin/integration/LoginIntegrationTest.kt
class LoginIntegrationTest {
private lateinit var viewModel: LoginViewModel
@BeforeTest
fun setup() {
val mockEngine = MockEngine { request ->
when (request.url.encodedPath) {
"/auth/login" -> {
val body = request.body.toString()
if (body.contains("test@example.com")) {
respond(
content = Json.encodeToString(LoginFixtures.loginResponse()),
status = HttpStatusCode.OK
)
} else {
respond(
content = Json.encodeToString(ErrorResponse("Invalid")),
status = HttpStatusCode.Unauthorized
)
}
}
else -> respond("", HttpStatusCode.NotFound)
}
}
val apiClient = ApiClient(mockEngine)
val dataSource = LoginRemoteDataSourceImpl(apiClient)
val repository = LoginRepositoryImpl(dataSource)
viewModel = LoginViewModel(repository)
}
@Test
fun `end to end login flow with valid credentials`() = runTest {
viewModel.state.test {
assertEquals(Uninitialized, awaitItem())
viewModel.login("test@example.com", "password")
assertEquals(Loading, awaitItem())
val successState = awaitItem()
assertTrue(successState is Success)
}
}
@Test
fun `end to end login flow with invalid credentials`() = runTest {
viewModel.state.test {
assertEquals(Uninitialized, awaitItem())
viewModel.login("wrong@example.com", "wrong")
assertEquals(Loading, awaitItem())
val failedState = awaitItem()
assertTrue(failedState is Failed)
}
}
}Back to Agents