Fai*_*sal 5 testing kotlin koin
使用 koin-2.0.1 进行 Android 测试,尽管每个测试分别通过,但无法同时测试所有 3 个测试。
class NumberFormatterUtilImplTest : KoinTest {
private val numberFormatterUtil: NumberFormatterUtilImpl by inject()
@Before
fun setUp() {
startKoin { modules(utilsModule) }
}
@Test
fun `does formatter returns two digit faction if supplied one digit value`() {
val result = numberFormatterUtil.getAdjustedCurrencyRate(18.0)
Assert.assertEquals(result, 18.00, 1.0)
}
@Test
fun `does formatter returns two digit faction if supplied multiple digits value`() {
val result = numberFormatterUtil.getAdjustedCurrencyRate(18.12343)
Assert.assertEquals(result, 18.12, 1.0)
}
@Test
fun `does formatter returns rounded two digit faction if supplied multiple digits value`() {
val result = numberFormatterUtil.getAdjustedCurrencyRate(18.12876)
Assert.assertEquals(result, 18.13, 1.0)
}
}
Run Code Online (Sandbox Code Playgroud)
运行类级别测试结果如下:
org.koin.core.error.KoinAppAlreadyStartedException: A Koin Application has already been started
Run Code Online (Sandbox Code Playgroud)
任何输入都会有帮助,谢谢。
laa*_*lto 17
一种常见的做法是将@Before
设置与@After
清理配对。您可以stopKoin()
在那里打电话,以便下一个电话startKoin()
再次工作:
@After
fun tearDown() {
stopKoin()
}
Run Code Online (Sandbox Code Playgroud)
在 @Before 和 @After 方法上调用 stopKoin() ,如下所示:
import com.my.example.appModule
import android.os.Build
import androidx.test.core.app.ApplicationProvider
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.test.KoinTest
import org.koin.test.inject
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import java.util.*
@Config(sdk = [Build.VERSION_CODES.LOLLIPOP])
@RunWith(RobolectricTestRunner::class)
class SomeRepositoryTest: KoinTest {
// Return Completable of RxJava
private val repository: SomeRepository by inject()
@Before
fun before() {
stopKoin() // to remove 'A Koin Application has already been started'
startKoin {
androidContext(ApplicationProvider.getApplicationContext())
modules(appModule)
}
}
@After
fun after() {
stopKoin()
}
@Test
fun testSomething() {
repository.insert("data").blockingAwait()
assert(true)
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5405 次 |
最近记录: |