This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class MyResult<T> { | |
sealed class Success<T>(val data: T) : MyResult<T>() { | |
class Network<T>(data: T) : Success<T>(data) | |
class Cache<T>(data: T) : Success<T>(data) | |
} | |
data class Failure<T>(val error: String) : MyResult<T>() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `hot flow - Turbine`() { | |
runBlocking { | |
MutableStateFlow("test").test { | |
assertThat(expectItem()).isEqualTo("test") | |
cancelAndIgnoreRemainingEvents() | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Unit Testing Cold Flows with Turbine | |
*/ | |
@Test | |
fun `cold flow - take multiple - Turbine`() { | |
runBlocking { | |
flow { | |
emit("test") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Unit Testing Hot Flows the Android recommended way | |
*/ | |
@Ignore | |
@Test | |
fun `hot flow - hangs`() { | |
runBlocking { | |
val actual = MutableStateFlow("test").single() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Unit Testing Cold Flows the Android recommended way | |
*/ | |
@Test | |
fun `cold flow - take multiple - Android recommended - Success!`() { | |
runBlocking { | |
val actual = mutableListOf<String>() | |
flow { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RxSchedulersOverrideRule(private val scheduler: Scheduler = TestScheduler()) : TestRule { | |
private val handler = Function<Scheduler, Scheduler> { scheduler } | |
override fun apply(base: Statement, description: Description): Statement = | |
object : Statement() { | |
override fun evaluate() { | |
RxJavaPlugins.reset() | |
RxAndroidPlugins.reset() | |
RxJavaPlugins.setIoSchedulerHandler(handler) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestCoroutineRule( | |
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher() | |
) : TestWatcher() { | |
override fun starting(description: Description?) { | |
super.starting(description) | |
Dispatchers.setMain(testDispatcher) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PeopleViewModelTest { | |
private lateinit var underTest: PeopleViewModel | |
private val service = mock<StarWarsService> { | |
onBlocking { getPeople() } | |
.then { | |
flow { | |
// Simulate multiple requests to backend | |
emit(Result.Success(listOf(dummyPerson))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.Observer | |
import androidx.lifecycle.asLiveData | |
import kotlinx.coroutines.flow.Flow | |
class LiveDataTestObserver<T> : Observer<T> { | |
val observedValues = mutableListOf<T>() | |
override fun onChanged(value: T) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FirebaseStorage.getInstance().reference.download("tanz.mov") | |
.collectIndexed { _, result: DownloadResult -> | |
delay(5000) | |
updateScreen(result) | |
} |
NewerOlder