question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"aMock wasNever called" treats mocks defined inside fixture objects as method calls

See original GitHub issue

We 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:

  1. Define a different syntax for verifications on a mock vs a method call. Maybe aMock wasNever interactedWith, or even just expose the regular verifyNoInteractions?

  2. 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)
    
  3. (Kinda orthogonal to the above) In Expect DSL, define on for mock objects on the right side, and that would be distinct from to which only expects method calls, so we’d use

    expect no calls on aMock
    

    instead of

    expect no calls to aMock
    

    on vs to would be such a subtle change though so I can imagine errors in these lines be frustrating to diagnose… Maybe a better alternative would be expect no interactions with aMock, or again, just expose the vanilla verifyNoInteractions/alias it to expectNoInteractions

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
bbonannocommented, Mar 26, 2020

I’ll play around with getting the macro to support this, you can play with the Expect DSL as it’s your baby 😉

0reactions
bbonannocommented, Mar 26, 2020

Gotcha

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocked methods created within `withObjectMocked` not ...
Since actors execute on the threads of the ActorSystem 's dispatcher (never in the calling thread), they cannot see such a mock.
Read more >
unittest.mock — mock object library — Python 3.11.1 ...
unittest.mock is a library for testing in Python. ... If wraps is not None then calling the Mock will pass the call through...
Read more >
Mocks Aren't Stubs - Martin Fowler
Mocks Aren't Stubs. The term 'Mock Objects' has become a popular one to describe special case objects that mimic real objects for testing....
Read more >
Mock Roles, not Objects - jMock
1. Create the test fixture including any mock objects. 2. Define expectations and stubs on the mock objects. 3. Invoke the method to...
Read more >
Using mock objects (Simple) | Instant Hands-on Testing with ...
It is very common for methods in a class to interact with other classes. ... If no other parameters are given, any method...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found