java.lang.IllegalStateException:缺少前面方法调用的行为定义

java.lang.IllegalStateException: missing behavior definition for the preceding method call

我有以下使用 Scala 2 的测试代码。11.x 与 Scalatest 和 EasyMock(也使用 EasyMockSugar):

import org.scalatest._
import org.easymock.EasyMock._
import org.scalatest.easymock._

// definition of Grid
trait Grid {
    def steps: Int
}

class MyTestSuite extends FunSuite with Matchers with EasyMockSugar {
  test("First differential correctness") {
    val grid: Grid = mock[Grid]
    val steps = 4
    expect(grid.steps).andReturn(steps)
    // use the grid mock ...
  }
}

但是,在运行时出现以下异常:

java.lang.IllegalStateException: missing behavior definition for the preceding method call:
Grid.steps()

您需要在模拟上调用 replay

class MyTestSuite extends FunSuite with Matchers with EasyMockSugar {
  test("First differential correctness") {
    val grid: Grid = mock[Grid]
    val steps = 4
    expect(grid.steps).andReturn(steps)

    replay(grid)
    // use the grid mock ...
    verify(grid)
  }
}