jeppeman / mockposable

Mocking with Jetpack Compose - stubbing and verification of Composable functions
Apache License 2.0
39 stars 3 forks source link

Can't draw #31

Open juliocbcotta opened 3 weeks ago

juliocbcotta commented 3 weeks ago

I am trying to mock composables and print the invocation args, but I am getting this.

Code

class MyClass {
    @Composable
    fun compose(text: String){
        Text(text)
    }
}
private fun mockMyClass(): MyClass {
    return mockk<MyClass> {
        everyComposable { compose(any()) } answersComposable {
            PrintArgs(it.invocation.args)
        }
    }
}

@Composable
private fun PrintArgs(args: List<Any?>) {
    Text(args.toString())
}

class MyClassTest {

   @Test
   fun test(){
       val my = mockMyClass()

       MaterialTheme {
          my.compose("test text")
      }
   }
}

Error

CompositionLocal LocalFontFamilyResolver not present
java.lang.IllegalStateException: CompositionLocal LocalFontFamilyResolver not present
    at androidx.compose.ui.platform.CompositionLocalsKt.noLocalProvidedFor(CompositionLocals.kt:242)
    at androidx.compose.ui.platform.CompositionLocalsKt.access$noLocalProvidedFor(CompositionLocals.kt:1)
    at androidx.compose.ui.platform.CompositionLocalsKt$LocalFontFamilyResolver$1.invoke(CompositionLocals.kt:128)
    at androidx.compose.ui.platform.CompositionLocalsKt$LocalFontFamilyResolver$1.invoke(CompositionLocals.kt:127)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at androidx.compose.runtime.LazyValueHolder.getCurrent(ValueHolders.kt:46)
    at androidx.compose.runtime.LazyValueHolder.readValue(ValueHolders.kt:48)
    at androidx.compose.runtime.CompositionLocalMapKt.read(CompositionLocalMap.kt:91)
    at androidx.compose.runtime.ComposerImpl.consume(Composer.kt:2375)
    at androidx.compose.foundation.text.BasicTextKt.BasicText-VhcvRP8(BasicText.kt:622)
    at androidx.compose.material3.TextKt.Text--4IGK_g(Text.kt:114)
    at com.veepee.vpcore.screenshot.testing.ThemedScreenshotExtKt.PrintArgs(ThemedScreenshotExt.kt:37)
jeppeman commented 3 weeks ago

Hi @juliocbcotta, have you included the compose-ui plugin?

mockposable {
    plugins = ['mockk', 'compose-ui']
}

If not, refer to https://github.com/jeppeman/mockposable?tab=readme-ov-file#stubbing-and-verification-with-compose-ui