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



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

详细信息:

所以我使用ScalaTest,这个测试涉及五个类。。。

我正在测试的子指令

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 + ".")
  }
}

构造机器对象所需的寄存器

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()

-

几个小时以来,我一直在寻找一种简单的方式来嘲笑这门课,但一无所获。目前,我已经确定了下面详细介绍的解决方法,但我的印象是,mocking可以为测试我的SubInstruction类提供一个不那么复杂的解决方案。

解决方法:

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

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帐户上找到完整的代码。

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

FWIW,Mockito对此表示支持。搜索RETURNS_DEEP_STUBS

相关内容

  • 没有找到相关文章

最新更新