1

I'm using scalamock to write a test. The problem is that action is asynchronous. I have the following pseudo code

val resultCollectorMock = mock[ResultCollector]
(resultCollectorMock.collectResult _).expect(someResult)

val serviceUnderTest = new ServiceUnderTest(resultColletorMock)
serviceUnderTest.runAsyncJob(someParams)

This fails because the result is computed asynchronously, at time when the test ends, it's still not ready, so collectResult wasn't called, yet.

What I want to have is expectEventually(value)(patienceConfig) that is able to wait for some time for the method to be called.

I tried to use a sutb and verify instead, I wrapped it in eventually from scalatest but to no avail. For whatever reason verify seems to break the test at first evaluation.

mjjaniec
  • 793
  • 1
  • 8
  • 19

1 Answers1

1

you should use AsyncMockFactory with an appropriate test suite and Futures as described in the docs at https://scalamock.org/user-guide/integration/

Philipp
  • 967
  • 6
  • 16
  • oh, now, I feel a bit embarrassed as I searched through spec I didn't notice it, perhaps I didn't expect to find it under `integration`. Anyway, it works like a charm, thank you! – mjjaniec Apr 16 '21 at 12:26
  • I have a test which wont work with AsyncMockFactory. I have to use `eventually`, I'm surprised stub verification fails it without time backoffs. – Basil Sep 02 '21 at 17:08