-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What about improving doReturn -> when implementation? #453
Comments
The problem with At no point in the chain do we know that we're "done" and can wrap it up. val myMock = mock<MyClass> {
on { myMethod() } doReturn "first" doReturn "second" doThrow IllegalStateException()
} Technically we could store the current stub in the KStubbing and close the stubbing the next time I would propose a different syntax, which provides symmetry with the existing implementation. It would be extremely easy to implement as well. val myMock = mock<MyClass> {
doReturn("first").on { myMethod() }
} I went with val myMock = mock<MyClass> {
// consistent dot notation
doReturn("first").doReturn("second").doThrow(IllegalStateException()).on { myMethod() }
// no dot notation is inconsistent and awkward after chaining
doReturn("first").doReturn("second").doThrow(IllegalStateException()) on { myMethod() }
} Implementing it would just require one extra method in fun Stubber.on(methodCall: T.() -> Unit) {
this.`when`(mock).methodCall()
} That method wouldn't provide type safety, but we could create a We could provide type safety and type inference given a different syntax, but it's awkward. val myMock = mock<MyClass> {
on({ myMethod() }) {
doReturn("first")
doReturn("second")
doThrow(IllegalStateException())
}
} There are probably other possible syntaxes, but these are the ones I can think of |
Co-authored-by: Kate Corcoran <[email protected]>
I believe there's a missed opportunity on the
doReturn.when
implementation. Mockito kotlin is only changing reseved word'when'
withwhenever
, but the implementation is not on par with the rest of the library.When using on
{ myMethod() } doReturn "something"
, myMethod is inferred from the mock scope. However the doReturn is not doing the same and we need to resend the mock to whenever method:A better format would be:
or even a format similar to the ones already there:
The same for
doNothing
and other similar methodsOr by a fact, i would've preferred that
on - doReturn
calleddoReturn.when
instead, I see no purpose on invoking the real methods and the need to use a different method for spies.The text was updated successfully, but these errors were encountered: