【发布时间】:2019-07-06 23:42:19
【问题描述】:
我正在测试我的演示者课程和所有测试工作。但是当我尝试运行我的测试类时,所有协程测试都失败了。
我正在尝试重置我的调度并清理我的范围。
private val dispatcher = TestCoroutineDispatcher()
private val testScope = TestCoroutineScope(dispatcher)
@Before
fun setUp() {
Dispatchers.setMain(dispatcher)
products = ProductsMotherObject.createEmptyModel()
getProductsUseCase = GetProductsUseCase(productsRepository)
updateProductsUseCase = UpdateProductsUseCase(productsRepository)
presenter = HomePresenter(view, getProductsUseCase, updateProductsUseCase, products)
}
@After
fun after() {
Dispatchers.resetMain()
testScope.cleanupTestCoroutines()
}
这是我的测试示例
@Test
fun `should configure recyclerview if response is success`() = testScope.runBlockingTest {
//Given
`when`(productsRepository.getProductsFromApi()).thenReturn(mutableMapOf())
//when
presenter.fetchProducts()
//then
verify(view).hideLoading()
verify(view).setUpRecyclerView(products.values.toMutableList())
}
我的测试中只有一个错误,但每个测试在单独运行时都有效
【问题讨论】:
标签: android unit-testing kotlin kotlin-coroutines