I would like to preview my screen content which ex...
# compose
l
I would like to preview my screen content which expects a view model/presenter (custom class) object. I have made interfaces for this but I'm a bit lost how to fake the presenter object or is the suggested way to instantiate the presenter object here directly (but which leads to a render problem)?
Copy code
@Preview
@Composable
private fun Preview() {
    MyTheme {
        DashboardContent(
            scaffoldState = rememberScaffoldState(),
            appBarTitle = "Preview",
            presenter = // TODO
        )
    }
}
f
I would suggest one of these: • create an interface that your presenter implements and then use a mocked implementation for the preview • wrap your composables using another composable that abstracts away the presenter, like so
Copy code
fun Composable(presenter: Presenter) {
    Composable(
        onButtonClick = { presenter.onButtonClick() },
        onSomethingElse = { presetner.onSometingElse() }
    )
}

fun Composable(
    onButtonClick: () -> Unit,
    onSometingElse: () -> Unit)
) {
    // whatever you have in your composable now goes here
}
l
@Francesc I like the second suggestion but I have a question about the first one: with
mock
do you mean something like this:
Copy code
val presenter = object : IPresenter {..}
If yes, my interface has more than 3 methods and properties, so it would be a pain to implement all of them. Or is there a library to mock such objects?
f
yes, by mock I mean creating an implementation of your interface with just stubs
l
sorry, what are stubs. I have read about this term in relation with testing but tbh I haven't touched anything regarding android testing and mocking yet. Am I on the right track with this snippet?
f
stub is just an empty implementation. If your method does not return anything, leave it empty (
fun foo(bar: Bar) = Unit
), otherwise return a token item, (
fun foobar(bar: Bar) = ""
)
l
ok got it. Thanks for your time