Using MockK library in Jetpack Compose Preview
dev.toΒ·17hΒ·
Discuss: DEV
Flag this post

Starting

What is Preview? It’s a functionality that gives your an opportunity for view a composable UI during development without the need to build and run on an emulator. (If you use the Android Studio obviously)

Problem

In fact you must have a some data that you wanna see in preview. But sometimes it’s impossible.

Imagine, you have a screen like this:

@Composable
fun SomeScreen(
viewModel: SomeViewModel = hiltViewModel(),
){
//some UI
}

And view model of that screen like this:

class SomeViewModel @Inject constructor(
private val someUseCase1: someUseCase1,
private val someUseCase2: someUseCase2
//etc
) : ViewModel() {

val someDataFLow1 = someUseCase1.getSomeData()
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initia...

Similar Posts

Loading similar posts...