Scalamock: Mocking call by-name function with arguments

596 views Asked by At

In following code snippet, I need to ensure that BinaryConverter#intToStr is invoked.

import org.scalamock.scalatest.MockFactory
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class Foo {
  def foo(x: Int)(b: => String): String = b
}
class BinaryConverter {
  def intToStr(x: Int): String = x.toBinaryString
}
class Converter(fooBar: Foo, converter: BinaryConverter) {
  def convert2Bin(x: Int): String = fooBar.foo(x)(converter.intToStr(x))
}
class FooTest extends AnyFlatSpec with Matchers with MockFactory {
  val mockFoo: Foo = mock[Foo]
  val mockBinConverter: BinaryConverter = mock[BinaryConverter]
  val converter = new Converter(mockFoo, mockBinConverter)

  behavior of "Foo"

  it should "mock foo doesn't work" in {
    (mockBinConverter.intToStr _).expects(2).returns("Mock 10")
    (mockFoo.foo(_: Int)(_: String)).expects(2, *).onCall(_.productElement(1).asInstanceOf[Int => String](2))

    converter.convert2Bin(2) shouldBe "Mock 10"
  }
}

I've tried to use onCall product but get Converter$$Lambda$132/182531396 cannot be cast to scala.Function1. Although, same code works when casting a parameterless function to Function0, .asInstanceOf[() => String]()

1

There are 1 answers

0
rojanu On

Now, I understand the mistake I was making; I was trying to cast the curried call-by-name argument of the Foo#foo to be the function that was placed in it, in above sample it's BinaryConverter#intToStr(x: Int): String.

Instead, parameter should be cast to () => String and then invoked so the code inside can be executed.

(mockFoo.foo(_: Int)(_: String)).expects(2, *).onCall(_.productElement(1).asInstanceOf[() => String]())