Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Koin mocking suspend function

Tags:

testing

koin

Can anybody tell me if/how I can mock a supend funktion with koin test? The only thing I know so far is this behavior

declareMock<...> {
      given(..)).willReturn(...)
   }

but this doesn't work on suspend fun(). Is there anything similar to 'coEvery' in Mockk, or how can I do this?

Thanx in advance,

Wolfgang

like image 426
Wolfgang Avatar asked Aug 07 '26 06:08

Wolfgang


1 Answers

Finally I found out how it works. You just can use any other mocking framework and use declare with that mock like so:

var preferenceRepository = mockk<PreferenceRepository>()


    @Before
    fun before() {
        startKoin {
            androidContext(ApplicationProvider.getApplicationContext())
        }
        declare {
            factory { preferenceRepository }
        }
    }
like image 52
Wolfgang Avatar answered Aug 10 '26 06:08

Wolfgang