Wolfgang
Wolfgang

Reputation: 511

Koin mocking suspend function

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

Upvotes: 3

Views: 489

Answers (1)

Wolfgang
Wolfgang

Reputation: 511

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 }
        }
    }

Upvotes: 3

Related Questions