Caballero
Caballero

Reputation: 12111

ScalaMock - "Unexpected call" error when the call is defined

So I have this test (unfortunately I can't include the full code and failed to reproduce a simplified version):

"create method" should {
    "return OK" in {
        val offerService: OfferService = mock[OfferService]
        (offerService.create _).expects(offer).returning(Future(Left(Created())))
        val controller = new OfferController(offerService)
        val request = FakeRequest(POST, "/offer").withJsonBody(Json.toJson(offer))
        val result: Future[Result] = call(controller.create, request)
        status(result) must be (201)
    }
}

that is failing with this error:

[info] create method
[info] - should return OK *** FAILED ***
[info]   Unexpected call: <mock-1> OfferService.create(Offer(PixelSlate))
[info]   
[info]   Expected:
[info]   inAnyOrder {
[info]     <mock-1> OfferService.create(Offer(PixelSlate)) once (never called - UNSATISFIED)
[info]   }
[info]   
[info]   Actual:
[info]     <mock-1> OfferService.create(Offer(PixelSlate)) (Option.scala:121)

Am I missing something obvious here? The mocked method is called only once. Why is it saying the method was called and then not called at the same time?

Upvotes: 0

Views: 1020

Answers (1)

Philipp
Philipp

Reputation: 977

Not really enough information to answer why this occurs. You don't show what offer is, yet the question hinges on the == contract of that type being content equality and not reference equality. If that type behaves funny, and is beyond your control, then use predicate matching instead: https://scalamock.org/user-guide/matching/

Upvotes: 0

Related Questions