https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

aiidziis

04/01/2020, 9:16 AM
Hey! Is it currently possible to test callbacks in
commonTest
?
I am trying to capture callback values in mock and compare them to expected one.
Copy code
@Test
    fun `When Error response, Completion must contain Error and Watcher must contain empty lists`() =
        runTest {
            if (isNative) return@runTest
            selectNetwork.testCase = DashboardSelectTestCase.ERROR
            val watcherMock = DashboardSectionsWatcherMock()
            val completionMock = DashboardSectionsCompletionMock()
            selectRepository.watchDashboardSections = watcherMock
            selectRepository.startWatcher()
            selectRepository.fetchDashboardSection("en_US", completionMock)
            assertEquals(expected = DashboardSelectData.error, actual = completionMock.lastValue)
            assertEquals(expected = mutableListOf(), actual = watcherMock.values)
        }
fetchDashboardSection
is not a
susped fun
because we plan to use this repository in iOS application, and afaik
suspend
is not allowed in Kotlin Native.
t

Thomas

04/01/2020, 11:08 AM
suspend fun
is allowed in Kotlin/Native. The issue is that it isn't added to the Objective C framework so you can't call it from Objective C or Swift code. As long as you only use this function in Kotlin code it is not an issue.
a

aiidziis

04/01/2020, 11:12 AM
Refactored and addeed
suspend fun
. Now I can test if I receive error, while performing fetch, but still can’t figure out how to test
watchDashboardSection
invocation.
Copy code
localDataSource.selectAllSections()
    .flowOn(Dispatchers.Default)
    .map(::prepareDashboard)
    .collect { watchDashboardSections?.invoke(it) }
Currently I am collecting from
Flow
and invoking callback with data.