Writing a test that uses coroutines interop? #657
-
Has anybody ever written a test which uses the coroutines interop? I was trying to write one with the following being mocked and the class under test interface FirebaseAuth {
val authState: StateFlow<FirebaseAuthState>
}
internal class AuthenticationRepositoryImpl(private val auth: FirebaseAuth) :
AuthenticationRepository {
override val authState: Observable<AuthState> =
auth.authState.asObservable()
.subscribeOn(ioScheduler)
.map { state ->
when (state) {
is FirebaseAuthState.Authenticated -> AuthState.Authenticated(
state.firebaseUser.toWolfPackUser()
)
FirebaseAuthState.Unauthenticated -> AuthState.Unauthenticated
}
}
} The following test seems to work on android, but hangs on iOS and struggling to figure out what's causing the test to hang? It looks like the internal class AuthenticationRepositoryTest : TestsWithMocks() {
override fun setUpMocks() = injectMocks(mocker)
@Mock
lateinit var firebaseAuth: FirebaseAuth
private val repository by withMocks { AuthenticationRepositoryImpl(firebaseAuth) }
@BeforeTest
fun before() {
overrideSchedulers(main = { TestScheduler() }, io = { TestScheduler() })
}
@Test
fun authState_IsAuthenticated() {
val firebaseUser: FirebaseUser =
fakeFirebaseUser().copy(uid = "some id", email = "some email", displayName = "my name")
every { firebaseAuth.authState } returns MutableStateFlow(
FirebaseAuthState.Authenticated(firebaseUser)
)
repository.authState
.test()
.assertValue(
AuthState.Authenticated(
WolfPackUser(
userId = firebaseUser.uid,
email = firebaseUser.email,
displayName = firebaseUser.displayName
)
)
)
} The test is using MocKmp for generating the mocks in the test by the way which works great when the class under test is all reaktive using a similar approach. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Do you use a stable version of coroutines or |
Beta Was this translation helpful? Give feedback.
Do you use a stable version of coroutines or
-native-mt
? What is the purpose ofsubscribeOn(ioScheduler)
in this particular case?