ЕДИНЦИОННЫЙ ТЕСТРИКА ПОЛУЧЕНИЯ РЕБЕНЕНИЯ С помощью CALLBACKFLOWAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 ЕДИНЦИОННЫЙ ТЕСТРИКА ПОЛУЧЕНИЯ РЕБЕНЕНИЯ С помощью CALLBACKFLOW

Сообщение Anonymous »

У меня есть следующий класс, который я пытаюсь проверить. Я использую Mockito, так как это библиотека, которую я использую < /p>
class ConsentUpdatedBroadcastReceiverImp @Inject constructor(
private val context: Context,
private val contentManager: ContentManager,
private val applicationScope: CoroutineScope,
) : ConsentUpdatedBroadcastReceiver {
override val observe: Flow
get() {
return callbackFlow {
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val consentStatusPreference = contentManager.getConsentStatusPreference()

trySend(consentStatusPreference).isSuccess
}
}

ContextCompat.registerReceiver(
context,
receiver,
IntentFilter(BroadcastServiceKeys.CONSENT_UPDATED),
ContextCompat.RECEIVER_NOT_EXPORTED,
)

awaitClose {
context.unregisterReceiver(receiver)
}
}.shareIn(
scope = applicationScope,
started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 5_000L),
)
}
}
< /code>
Это мой модульный тест < /p>
class ConsentUpdatedBroadcastReceiverImpTest {

private val ontentManager = mock()
private val testDispatcher = StandardTestDispatcher()
private val applicationScope = TestScope(testDispatcher)
private val context = mock()
private lateinit var consentUpdatedBroadcastReceiverImp: consentUpdatedBroadcastReceiverImp

@Before
fun setUp() {
consentUpdatedBroadcastReceiverImp = ConsentUpdatedBroadcastReceiverImp(
context = context,
contentManager = contentManager,
applicationScope = applicationScope
)
}

@Test
fun `given after receiving a broadcast then send broadcast`() {
runTest {
// Given
val consentStatusPreference = mock {
on { performance } doReturn ConsentStatusType.DENIED
on { functionality } doReturn ConsentStatusType.ACCEPTED
on { targeting } doReturn ConsentStatusType.NOT_COLLECTED
}
whenever(contentManager.getConsentStatusPreference()).thenReturn(consentStatusPreference)

var registeredReceiver: BroadcastReceiver? = null
whenever(context.registerReceiver(any(), any(), any(), anyOrNull())).thenAnswer {
registeredReceiver = it.getArgument(1)
null
}

// When
consentUpdatedBroadcastReceiverImp.observe.test {
registeredReceiver?.onReceive(context, Intent(OTBroadcastServiceKeys.OT_CONSENT_UPDATED))
val _consentStatusPreference = this.awaitItem()

// Then
assert(_consentStatusPreference === consentStatusPreference)
cancelAndIgnoreRemainingEvents()
}
}
}
}
< /code>
Это ошибка при запуске теста < /p>
No value produced in 3s
app.cash.turbine.TurbineAssertionError: No value produced in 3s
at app//app.cash.turbine.ChannelKt.awaitEvent(channel.kt:89)
at app//app.cash.turbine.ChannelKt$awaitEvent$1.invokeSuspend(channel.kt)
at app//kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at app//kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
at app//kotlinx.coroutines.test.TestDispatcher.processEvent$kotlinx_coroutines_test(TestDispatcher.kt:24)
at app//kotlinx.coroutines.test.TestCoroutineScheduler.tryRunNextTaskUnless$kotlinx_coroutines_test(TestCoroutineScheduler.kt:99)
at app//kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTest$2$1$workRunner$1.invokeSuspend(TestBuilders.kt:322)
at app//kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)


Подробнее здесь: https://stackoverflow.com/questions/796 ... llbackflow
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»