https://kotlinlang.org logo
Title
h

Hari K

10/18/2021, 8:15 AM
Hi All, Someone help me how to mock this in KMM androidTest
Clock.System.now()
g

Grégory Lureau

10/18/2021, 8:27 AM
I'd wrap the call in a class (I've a TimeProvider in my current project to handle that, with a method
now()
) so that I can mock the class and pass the value I want without thinking about the Kotlin stdlib mock-ability.
👍 2
h

hfhbd

10/18/2021, 9:04 AM
You could simple implement the
Clock
interface and increase the internal counter in your test.
@ExperimentalTime
class MockClock(private var durationSinceEpoch: Duration = Duration.ZERO) : Clock {
    override fun now(): Instant = Instant.fromEpochMilliseconds(durationSinceEpoch.inWholeMilliseconds)

    operator fun plusAssign(duration: Duration) {
        durationSinceEpoch + duration
    }
}