lupuuss / Mokkery

The mocking library for Kotlin Multiplatform, easy to use, boilerplate-free and compiler plugin driven.
https://mokkery.dev
Apache License 2.0
202 stars 8 forks source link

Mocking outside of mock block #58

Closed bDev10 closed 2 weeks ago

bDev10 commented 2 weeks ago

Seems like mocking outside of mock() function's block parameter is not working, although documentation suggests it should work. E.g. for:

val repository = mock<BookRepository>()
every { repository.findAll() } returns flowOf(Book(...))

We get error:

Call BookRepository(1).findAll() not mocked!

When every is called from the block parameter, mocking works:

val repository = mock<BookRepository>() {
    every { findAll() } returns flowOf(Book(...))
}

Checked on version 2.5.0.

Would be useful to have this feature working e.g. for cases where mocked instance is created outside of test function and we want specify mocking behavior expected for specific test case.

lupuuss commented 2 weeks ago

Hi! Could you provide me a reproducer? I'm not able to reproduce it based on your samples.

bDev10 commented 2 weeks ago

Hi, I'm sorry it was my fault, so false alarm. Not spot that the class with injected mock called the yet unmocked function in init block, so just mock was called earlier then I expected when I wrote the test and the error was legit!