使用ScalaMock在模拟类上测试嵌套方法调用

时间:2015-02-13 23:49:01

标签: scala unit-testing mocking scalatest scalamock

我是ScalaMock的新手和一般的嘲笑。我试图测试一个方法,该方法在另一个(模拟的)类中调用一个方法,然后在返回的对象上调用一个方法。

详细信息:

所以我使用的是ScalaTest,这个测试涉及五个类......

我正在测试的SubInstruction

class SubInstruction(label: String, val result: Int, val op1: Int, val op2: Int) extends Instruction(label, "sub") {

  override def execute(m: Machine) {
    val value1 = m.regs(op1)
    val value2 = m.regs(op2)
    m.regs(result) = value1 - value2
  }
}

object SubInstruction {
  def apply(label: String, result: Int, op1: Int, op2: Int) =
    new SubInstruction(label, result, op1, op2)
}

必须模拟测试的机器

case class Machine(labels: Labels, prog: Vector[Instruction]) {
  private final val NUMBEROFREGISTERS = 32
  val regs: Registers = new Registers(NUMBEROFREGISTERS)

  override def toString(): String = {
    prog.foldLeft("")(_ + _)
  }

  def execute(start: Int) =
    start.until(prog.length).foreach(x => prog(x) execute this)
}

object Machine extends App {
  if (args.length == 0) {
    println("Machine: args should be sml code file to execute")
  } else {
    println("SML interpreter - Scala version")

    val m = Translator(args(0)).readAndTranslate(new Machine(Labels(), Vector()))

    println("Here is the program; it has " + m.prog.size + " instructions.")
    println(m)
    println("Beginning program execution.")
    m.execute(0)
    println("Ending program execution.")
    println("Values of registers at program termination:")
    println(m.regs + ".")
  }
}

构建Machine对象所需的注册

case class Registers(size: Int) {
  val registers: Array[Int] = new Array(size)

  override def toString(): String =
    registers.mkString(" ")

  def update(k: Int, v: Int) = registers(k) = v
  def apply(k: Int) = registers(k)
}

我作为原始Machine类创建的MockableMachine没有空构造函数,因此(据我所知)不能被模拟

class MockableMachine extends Machine(Labels(), Vector()){

}

最后是我的测试类SubInstructionTest,它编译但会抛出下面的异常。

class SubInstructionTest extends FlatSpec with MockFactory with Matchers {

  val label1 = "f0"
  val result1 = 25
  val op1_1 = 24
  val op2_1 = 20
  val sub1 = SubInstruction(label1, result1, op1_1, op2_1)

  "A SubInstruction" should "retrieve the operands from the correct registers in the given machine " +
    "when execute(m: Machine) is called, and perform the operation saving the " +
    "result in the correct register." in {
    val mockMachine = mock[MockableMachine]

    inSequence {
      (mockMachine.regs.apply _).expects(op1_1).returning(50)
      (mockMachine.regs.apply _).expects(op2_1).returning(16)
      (mockMachine.regs.update _).expects(result1, 34)
    }
    sub1.execute(mockMachine)

  }

}

抛出:

java.lang.NoSuchMethodException:Registers.mock $ apply $ 0()

-

我一直在寻找一种直接的方式来嘲笑这个课几个小时,但什么都没发现。目前我已经确定了下面详述的解决方法,但我的印象是,模拟会为测试我的SubInstruction类提供一个不那么复杂的解决方案。

解决方法:

删除MockableMachine类并创建一个CustomMachine类,该类扩展Machine并用构造时提供的mockedRegisters替换寄存器值。

class CustomMachine (mockedRegister: Registers) extends Machine(Labels(), Vector()) {
  override
  val regs: Registers = mockedRegister
}

我作为原始创建的MockableRegisters类没有空构造函数,因此(据我所知)不能被模拟

class MockableRegisters extends Registers(32) {

}

和SubInstructionTest类以稍微不同的方式编写

class SubInstructionTest extends FlatSpec with MockFactory with Matchers {

  val label1 = "f0"
  val result1 = 25
  val op1_1 = 24
  val op2_1 = 20
  val sub1 = SubInstruction(label1, result1, op1_1, op2_1)

  "A SubInstruction" should "retrieve the operands from the correct registers in the given machine " +
    "when execute(m: Machine) is called, and perform the operation saving the " +
    "result in the correct register." in {
    val mockRegisters = mock[MockableRegisters]
    val machine = new CustomMachine(mockRegisters)

    inSequence {
      (mockRegisters.apply _).expects(op1_1).returning(50)
      (mockRegisters.apply _).expects(op2_1).returning(16)
      (mockRegisters.update _).expects(result1, 34)
    }
    sub1.execute(machine)

  }

}

如上所示,这对我来说就像是一种解决方法,是不是有一种更简单的方法(可能与我原来的尝试相似)?

我刚刚提供了提问的基本代码,但您可以在GitHub account找到完整的代码。

1 个答案:

答案 0 :(得分:1)

我不认为Scalamock会隐式支持模拟嵌套对象。您必须模拟第一次调用返回的对象,这就是您的工作示例。

FWIW,Mockito支持这一点。搜索RETURNS_DEEP_STUBS