lupuuss / Mokkery

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

Provide way to return a computed value not just a straight value with returns/returnsSuccess #17

Closed dalewking closed 2 weeks ago

dalewking commented 1 month ago

I have a lot of boilerplate mocks set up like this where I have a bunch of var properties defining the data which can then be modified and then would like to use returns/returnsSuccess with the property instead of call.

So using the example from the docs it currently looks something like this:

var book = Book(1)

val repository = mock<BookRepository> {
   everySuspend { findById(any()) } calls { book }
}

reading the code it doesn't really represent what it is doing. calls is only used because we need to get the current value of the property. And when the return value is result there is no callsSuccess

So my thought would be that there would be overloads of returns/returnsSuccess that instead of a value of type T take a lambda of type () -> T

Then one could instead say something more like

val repository = mock<BookRepository> {
   everySuspend { findById(any()) } returns { book }
}

or

val repository = mock<BookRepository> {
   everySuspend { findById(any()) } returns(::book)
}

and for the case where the type is a Result

val repository = mock<BookRepository> {
   everySuspend { findById(any()) } returnsSuccess { book }
}
lupuuss commented 1 month ago

I like the idea, but I would adjust the naming:

every { mock.findById(any()) } returnsBy ::book
every { mock.findById(any()) } returnsSuccessBy ::book
every { mock.findById(any()) } returnsFailureBy ::IllegalStateException
every { mock.findById(any()) } throwsBy ::IllegalStateException
lupuuss commented 2 weeks ago

It's implemented. It will be available with the next release.