"aMock wasNever called" treats mocks defined inside fixture objects as method calls
See original GitHub issueWe use fixture objects (not traits) and org.scalatest.fixture._
specs, so mocks are provided into the test like so
class Fixture {
val service = mock[Service]
}
// ...
"mySpec" should "test something" in { f: Fixture =>
f.service wasNever called
}
VerifyMacro
treats f.service
as a method call, and I get a NotAMockException
.
I can think of a few ways to solve it:
-
Define a different syntax for verifications on a mock vs a method call. Maybe
aMock wasNever interactedWith
, or even just expose the regularverifyNoInteractions
? -
I don’t think we can distinguish a method call from a field access using quasiquotes -
q"$a.$b"
matches both if the method has no parens or type args. Here’s what I think we could do, however:- Method calls with parens is always
Apply
tree - Field access or no-paren method calls is always
Select
We could match against these types specifically, and in the case of
Select
, check if the penultimate identifier is a mock - and if so, treat it as a method call, otherwise as field access. To illustrate:fixture.aMock wasNever called
translates into
if (mockingDetails(fixture).isMock) verify(fixture, never()).aMock else verifyZeroInteractions(fixture.aMock)
- Method calls with parens is always
-
(Kinda orthogonal to the above) In Expect DSL, define
on
for mock objects on the right side, and that would be distinct fromto
which only expects method calls, so we’d useexpect no calls on aMock
instead of
expect no calls to aMock
on
vsto
would be such a subtle change though so I can imagine errors in these lines be frustrating to diagnose… Maybe a better alternative would beexpect no interactions with aMock
, or again, just expose the vanillaverifyNoInteractions
/alias it toexpectNoInteractions
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
I’ll play around with getting the macro to support this, you can play with the Expect DSL as it’s your baby 😉
Gotcha