ScalaMock verifying a generic method returning Unit gives method overloading compile error

1.4k views Asked by At

This following code:

import org.scalamock.scalatest.MockFactory
import org.scalatest.FlatSpec

case class Container[T](value: T)

trait Service[T] {
  def doWork(value: T): Unit
}

class DoesMatch[T](service: Service[T]) {
  def doMatch(container: Container[T]) = container match {
    case Container(value) => service.doWork(value)
  } 
}

class TestScalaMock extends FlatSpec with MockFactory {
  "Scala Mock" should "verify my call" in {
    val stubService = stub[Service[Int]]
    val matcher = new DoesMatch[Int](stubService)
    val container = Container(2)
    matcher.doMatch(container)
    (stubService.doWork _).verify(container)
  }
}

Gives a compile error "overloaded method value verify with alternatives: (matcher: org.scalamock.FunctionAdapter1[Int,Booleam]) org.scalamock.CallHandler1[Int,Unit] with org.scalamock.Verify (v1: org.scalamock.MockParameter[Int]") org.scalamock.CallHandler1[Int,Unit] with org.scalamock.Verify cannot be applied to (x.y.Container[Int])"

Removing the generics does not help. Swapping from a stub/verify to a mock/expects gives the same style compile error.

I am using scala 2.11.0 with

  "org.scalatest" % "scalatest_2.11" % "2.2.1" % "test",
  "org.scalamock" %% "scalamock-scalatest-support" % "3.2" % "test"

Any help much appreciated.

1

There are 1 answers

0
Eugene Zhulenev On BEST ANSWER

You've got wrong type for 'verify' method, it should be Int. stubService is type of Service[Int] and container is also Container[Int] => in DoesMatch you pass Int to service

(stubService.doWork _).verify(container.value)