-
Notifications
You must be signed in to change notification settings - Fork 57
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
New "expect" DSL promoting safer prefixed verifications, fixes #198 #205
New "expect" DSL promoting safer prefixed verifications, fixes #198 #205
Conversation
""""some value" willBe thrown by org.bar""" shouldNot compile | ||
} | ||
|
||
"check a mock was not used" in { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from here on it's basically a copy of IdiomaticMockitoTest
with updated DSL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we split the test and create a "Stubbings" one to avoid copy/pasting all the first part? Basically to also follow the split you made in the code :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!
It might be hard to split them cleanly though, because some stubbing tests seem to rely on verifications too...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could start with the ones that require no verification and then review the rest, or just pick one verification syntax, or even use the traditional one via the Mockito
object so we don't favour a specific syntax.
Maybe that could be a different PR, for this one I think that with the README changes and a minor bump on the version.txt
we should be good to go, being a new feature we have little risk of breaking exisiting clients.
Nice, this makes errors harder to make indeed. Some opinion below but feel free to ignore :)
At the same time, I'm not sure how to join this with more detailed expectations (like |
@Krever thanks for looking at it and sharing the feedback! I agree that it ended up a bit verbose. I originally tackled this initiative with something like That said, if As for the I'm definitely open to changing it to whatever better/shorter/less verbose DSL we can find - as long as we can keep "expect/verify" context at the front. |
@einholen Nice contrib! I like it already :) Sorry for the delay on feedback but been quite busy lately |
@@ -9,3 +9,14 @@ trait IdiomaticMockito extends IdiomaticMockitoBase { | |||
* Simple object to allow the usage of the trait without mixing it in | |||
*/ | |||
object IdiomaticMockito extends IdiomaticMockito | |||
|
|||
// TODO need a better name | |||
trait IdiomaticMockito2 extends IdiomaticStubbing with PrefixExpectations { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bbonanno do you have thoughts on a better name? I'm thinking maybe it shouldn't be a base trait for now, or maybe we can hide it in the companion object and call something like trait IdiomaticMockito.WithExpect
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd follow the spirit of Scalatest once again and leave them separated, and maybe deprecate IdiomaticMockito
in favour of IdiomaticStubbing with PostfixExpectations
or whatever the user wants to use, altough for people using warnings as errors it could be quite annoying (assuming they have a lot of tests to refactor), so not 100% about deprecating it in the code yet, but definetively express in the README what the new preferred way is.
it feels like this deserves a major version bump, especially if |
@jozic That's why I wasn't planing to deprecate it (yet), but I agree, if we do such breaking change it will require a major version bump, maybe the best is to mark the new API as Experimental until a couple of people actually use it in a project (I found out myself that while using the lib I realised of a lot of problems or improvements to be made) @einholen Do you agree? if so, can you do the small changes in the code (remove With that and a README entry I'd be happy to merge it in and start the work towards mockito-scala 2.x.x |
Yes, totally - I wouldn't wanna do the major version bump before we have the new syntax as experimental for a while. I'll push the proposed changes and the readme update within a couple days! |
…o#198 With existing postfix verifications, it is easy to overlook a missing verification when method signatures are long, so "was called" statements can be hard to spot - and unfortunately, in a lot of cases it means that a test that should be failing starts to pass. New DSL helps with this situation by sort of going back to the roots of vanilla Mockito, where "verify" word is defined before the verified method signature. Macros help us solve the inconvenience of having to wrap the mock variable with parentheses. Better naming wanted for IdiomaticMockito2 :)
- rearrange base traits - replace "IdiomaticMockito2" with "IdiomaticMockito.WithExpect" + "experimental" comments - extract non-verification tests from expect/verify tests (but more can be done) - bump version and update readme
6263256
to
39c3bcf
Compare
Apologies for the delay, I was traveling last weekend, and it was hard to find time to work on the PR amidst regular work and coronavirus panic. @bbonanno I think I addressed your comments!
|
With existing postfix verifications, it is easy to overlook a missing verification when method signatures are long, so
was called
postfixes can be hard to spot - and unfortunately, in a lot of cases it means that a test that should be failing starts to pass.New DSL helps combat this situation by sort of going back to the roots of vanilla Mockito, where "verify" word is defined before the verified method signature. Macros help us solve the inconvenience of having to wrap the mock variable with parentheses.
I wanted to reuse as much code as possible, and in order to do that I needed to restructure
IdiomaticMockitoBase
a bit: I split it intoIdiomaticStubbing
+PostfixVerifications
traits, so a proper idiomatic base can now be composed ofIdiomaticStubbing
+PostfixVerifications
/PrefixExpectations
However, better naming still wanted for
IdiomaticMockito2
🙂A few examples comparing
was called
DSL withexpect a call
DSL:So far I believe the version in this PR supports everything that existing DSL can.
My macros are a bit rusty, so I'm not 100% confident that all the pattern match cases are correct - but all existing tests do pass.