0

I'm working with ScalaMock and ScalaTest to put together some unit tests. I want to use the ScalaMock's expectation syntax to confirm that the service I'm working on passes along acceptable Json to a mocked resource. In this case acceptable means some internal fields can be different, so I've written a custom function to determine whether the fields I care about match. The problem with this is that it produces relatively unhelpful logging when the expectations are not fulfilled. The expectations set as in:

(mockedResource.doAThing(_: Json)(_: SomeImplicitThing))
  .expects(
    where(
      jsonSimilarAndImplicitEqual(
        expectedJson,
        anInstanceOfImplicitThing
      )(_, _)
    )
  )

Produce the following logging on failure:

- should forward acceptable Json to the mockedResource
  Unsatisfied expectation:

  Expected:
  [info]   inAnyOrder {
  [info]     <mock-1> MockedResource.doAThing<function1> once (never called - UNSATISFIED)
  [info]   }

I'd like to be able to provide some custom logging function to describe why jsonSimilarAndImplicitEqual evaluated to false in a sensible place. I was hoping an inAnyOrderWithLogging block would prove sufficient, but it turns out I can't even use this construct, since it is not supported for AsyncMockFactory.

1 Answers1

-1

You could use specs2-mock (a wrapper for Mockito) instead of ScalaMock, and use an argument captor. Using the org.specs2.Mockito trait, you can write val captor = capture[String] and then there was one(value).method(captor.capture). Once you've captured the argument, you can use scalatest Matchers to do assert the relevant properties.

tilde
  • 848
  • 8
  • 19