|
1 | 1 | package com.bangbang93.openbmclapi |
2 | 2 |
|
3 | | -import com.bangbang93.openbmclapi.agent.module |
| 3 | +import com.bangbang93.openbmclapi.agent.AppModule |
| 4 | +import com.bangbang93.openbmclapi.agent.config.ClusterConfig |
| 5 | +import com.bangbang93.openbmclapi.agent.model.Counters |
| 6 | +import com.bangbang93.openbmclapi.agent.storage.IStorage |
4 | 7 | import io.ktor.client.request.get |
5 | 8 | import io.ktor.client.statement.bodyAsText |
6 | 9 | import io.ktor.http.HttpStatusCode |
7 | 10 | import io.ktor.server.testing.testApplication |
| 11 | +import io.mockk.coEvery |
| 12 | +import io.mockk.mockk |
| 13 | +import kotlinx.coroutines.runBlocking |
| 14 | +import org.koin.core.context.GlobalContext |
| 15 | +import org.koin.core.context.startKoin |
| 16 | +import org.koin.core.context.stopKoin |
| 17 | +import org.koin.dsl.module |
| 18 | +import org.koin.ksp.generated.module |
| 19 | +import kotlin.test.AfterTest |
| 20 | +import kotlin.test.BeforeTest |
8 | 21 | import kotlin.test.Test |
9 | 22 | import kotlin.test.assertEquals |
10 | 23 | import kotlin.test.assertTrue |
| 24 | +import com.bangbang93.openbmclapi.agent.module as appModule |
11 | 25 |
|
12 | 26 | class ApplicationTest { |
| 27 | + @BeforeTest |
| 28 | + fun setupKoin() { |
| 29 | + // 启动全局 Koin 容器(与当前初始化职责一致:main 启动,测试手动启动) |
| 30 | + startKoin { |
| 31 | + modules( |
| 32 | + AppModule().module, |
| 33 | + module { |
| 34 | + single { |
| 35 | + ClusterConfig( |
| 36 | + clusterId = "test-cluster", |
| 37 | + clusterSecret = "test-secret", |
| 38 | + ) |
| 39 | + } |
| 40 | + single<IStorage> { |
| 41 | + mockk<IStorage> { |
| 42 | + coEvery { check() } returns true |
| 43 | + coEvery { init() } returns Unit |
| 44 | + } |
| 45 | + } |
| 46 | + single { Counters() } |
| 47 | + }, |
| 48 | + ) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @AfterTest |
| 53 | + fun tearDownKoin() { |
| 54 | + // 清理全局容器,防止测试间相互影响 |
| 55 | + if (GlobalContext.getOrNull() != null) stopKoin() |
| 56 | + } |
| 57 | + |
13 | 58 | @Test |
14 | 59 | fun `主页返回OK`() = |
15 | 60 | testApplication { |
16 | | - // Arrange |
| 61 | + // Arrange:直接调用真实 module()(suspend),以扩展接收者形式传入 Application |
17 | 62 | application { |
18 | | - module() |
| 63 | + runBlocking { this@application.appModule() } |
19 | 64 | } |
20 | 65 |
|
21 | 66 | // Act |
|
0 commit comments