Skip to content

Instantly share code, notes, and snippets.

View marcinOz's full-sized avatar
💙

Marcin marcinOz

💙
View GitHub Profile
class TestContextProvider : CoroutineContextProvider() {
override val Main: CoroutineContext = Dispatchers.Unconfined
override val IO: CoroutineContext = Dispatchers.Unconfined
}
class TestCoroutineRule : TestRule {
private val testCoroutineDispatcher = TestCoroutineDispatcher()
private val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher)
override fun apply(base: Statement, description: Description?) = object : Statement() {
@Throws(Throwable::class)
override fun evaluate() {
Dispatchers.setMain(testCoroutineDispatcher)
base.evaluate()
class MyViewModelTest {
@get:Rule
val instantExecutorRule = InstantTaskExecutorRule()
@get:Rule
val testCoroutineRule = TestCoroutineRule()
private lateinit var myViewModel: MyViewModel
@Mock
open class CoroutineContextProvider @Inject constructor() {
open val Main: CoroutineContext by lazy { Dispatchers.Main }
open val IO: CoroutineContext by lazy { Dispatchers.IO }
}
class MyViewModel(
private val getDataUseCase: GetDataUseCase,
private val contextProvider: CoroutineContextProvider
) : ViewModel() {
private val handler = CoroutineExceptionHandler { _, exception ->
stateLiveData.value = MyViewState.Error(exception)
}
private val stateLiveData = MutableLiveData<MyViewState>()
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
Widget build(BuildContext context) {
return Column(children: [
Text(mainText),
...buildMainElements(),
if (page != pages.last)
FlatButton(child: Text('Next')),
for (var section in sections)
HeadingAction(section.heading),
]);
}
mixin Wings {
String speed = "fast"
void fly() {
print('Flying $speed')
}
}
class Bird extends Animal with Wings
class Bird(wings: Flying) : Animal, Flying by wings