Abhimanyu
09/01/2023, 5:56 PM@After
fun tearDown() = testScope.runTest {
testDataStore.edit {
it.clear()
}
testScope.cancel()
}
Getting error
Only a single call toAs both thecan be performed during one test.runTest
test
method and tearDown()
uses runTest
Stackoverflow question - https://stackoverflow.com/questions/77025162/data-store-clean-up-in-unit-test-error-only-a-single-call-to-runtest-can-be
Please help.
Thankssolonovamax
09/01/2023, 6:03 PMPeter
09/01/2023, 7:34 PMrunBlocking()
is a valid use case for this. From documentation:
It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and in tests.
And I think this is probably a valid case, where you need to bridge regular blocking code. JUnit doesn't support suspending. Alternatively, you could probably also make a helper function, that does cleanup.
@Test
fun someTest() {
runTestAndCleanup {
// test code
}
}
private fun runTestAndCleanup(block: suspend () -> Unit) = runTest {
try {
block()
} catch(e) {
TODO("cleanup")
throw e
}
}
Abhimanyu
09/01/2023, 7:52 PM