Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock a call-by-name argument (like getOrElse) using ScalaMock?

I'd like to be able to mock a return value of getOrElse method so that it returns what is passed as orElse call-by-name argument with ScalaMock

trait ToBeMocked {
  def getOrElse(arg: Int)(orElse: => String): String
}

ScalaMock has to be used because of currying. Running this:

class CallByNameMockSpec extends Specification with MockFactory {

  trait ToBeMocked {
    def getOrElse(arg: Int)(orElse: => String): String
  }

  "Mocking functions with call-by-name arguments" should {
    "work" in {
      val m = mock[ToBeMocked]

      (m.getOrElse (_: Int)(_: String)).expects(101, *).onCall((x, y) => y)

      m.getOrElse(101)("result") must beEqualTo("result")
    }
  }

}

throws an exception:

[error]     ClassCastException:        
test.service.CallByNameMockSpec$$anonfun$1$$anonfun$apply$1$$anonfun$apply$3$$anonfun$apply$4 cannot be cast to java.lang.String (CallByNameMockSpec.scala:16)
like image 254
Sergei Poganshev Avatar asked Aug 18 '13 01:08

Sergei Poganshev


1 Answers

The trick is to use Product version of onCall method, convert the second argument to () => String and call it:

class CallByNameMockSpec extends Specification with MockFactory {

  trait ToBeMocked {
    def getOrElse(arg: Int)(orElse: => String): String
  }

  "Mocking functions with call-by-name arguments" should {
    "work" in {
      val m = mock[ToBeMocked]

      (m.getOrElse (_: Int)(_: String)).expects(101, *).onCall(_.productElement(1).asInstanceOf[() => String]())

      m.getOrElse(101)("result") must beEqualTo("result")
    }
  }

}
like image 56
Sergei Poganshev Avatar answered Nov 03 '22 03:11

Sergei Poganshev