|
| 1 | +package io.ably.lib.objects.unit.setup |
| 2 | + |
| 3 | +import io.ably.lib.objects.integration.setup.ensureAttached |
| 4 | +import io.ably.lib.realtime.AblyRealtime |
| 5 | +import io.ably.lib.realtime.Channel |
| 6 | +import io.ably.lib.realtime.ChannelState |
| 7 | +import io.ably.lib.types.ClientOptions |
| 8 | +import io.mockk.every |
| 9 | +import io.mockk.mockk |
| 10 | +import io.mockk.spyk |
| 11 | +import org.junit.After |
| 12 | +import org.junit.runner.RunWith |
| 13 | +import org.junit.runners.BlockJUnit4ClassRunner |
| 14 | + |
| 15 | +// This class serves as a base for unit tests related to Live Objects. |
| 16 | +// It can be extended to include common setup or utility methods for unit tests. |
| 17 | +@RunWith(BlockJUnit4ClassRunner::class) |
| 18 | +open class UnitTest { |
| 19 | + |
| 20 | + private val realtimeClients = mutableMapOf<String, AblyRealtime>() |
| 21 | + internal fun getMockRealtimeChannel(channelName: String, clientId: String = "client1"): Channel { |
| 22 | + val client = realtimeClients.getOrPut(clientId) { |
| 23 | + AblyRealtime(ClientOptions().apply { |
| 24 | + autoConnect = false |
| 25 | + key = "keyName:Value" |
| 26 | + this.clientId = clientId |
| 27 | + }) |
| 28 | + } |
| 29 | + val channel = client.channels.get(channelName) |
| 30 | + return spyk(channel) { |
| 31 | + every { attach() } answers { |
| 32 | + state = ChannelState.attached |
| 33 | + } |
| 34 | + every { detach() } answers { |
| 35 | + state = ChannelState.detached |
| 36 | + } |
| 37 | + every { subscribe(any<String>(), any()) } returns mockk(relaxUnitFun = true) |
| 38 | + every { subscribe(any<Array<String>>(), any()) } returns mockk(relaxUnitFun = true) |
| 39 | + every { subscribe(any()) } returns mockk(relaxUnitFun = true) |
| 40 | + }.apply { |
| 41 | + state = ChannelState.attached |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @After |
| 46 | + fun afterEach() { |
| 47 | + realtimeClients.clear() |
| 48 | + } |
| 49 | +} |
0 commit comments