我在玩ScalaMock。我写了一个KISS版本,当我尝试使用ScalaMock模拟测试用例中的对象时,我面临的错误。
import akka.http.scaladsl.server.Route
import org.scalamock.scalatest.MockFactory
import org.scalatest.FlatSpec
import akka.http.scaladsl.testkit.ScalatestRouteTest
import akka.http.scaladsl.server.Directives._
import org.scalatest.Matchers._
trait MetricsRegistry {
def recordValue(myString: String): Unit
def decatMyString(myString: String): Unit
}
class ScalaMockError01Spec extends FlatSpec with MockFactory with ScalatestRouteTest {
def actorRefFactory = system
lazy val metricsRegistry = mock[MetricsRegistry]
val route : Route = {
get{
complete("Status is OK")
}
}
"MetricDirectives" should "record latency and success count for 200 response" in {
inAnyOrder {
(metricsRegistry.recordValue _).expects(*).twice
}
Get("/success") ~> route ~> check {
status.toString() shouldBe "200 OK"
}
}
}
我得到的错误如下
未满足预期:
预期:inAnyOrder { inAnyOrder { MetricsRegistry.recordValue(*) 两次(从未调用 - 不满意) } }
实际: ScalaTestFailureLocation: ScalaMockError01Spec at (ScalaMockError01Spec.scala:22) org.scalatest.exceptions.TestFailedException: 未满足的期望:
预期:inAnyOrder { inAnyOrder { MetricsRegistry.recordValue(*) 两次(从未调用 - 不满意) } }
实际: at org.scalamock.scalatest.AbstractMockFactory$class.newExpectationException(AbstractMockFactory.scala:51) 在 ScalaMockError01Spec.newExpectationException(ScalaMockError01Spec.scala:22) 在 ScalaMockError01Spec.newExpectationException(ScalaMockError01Spec.scala:22) 在 org.scalamock.context.MockContext$class.reportUnsatisfiedExpectation(MockContext.scala:45) 在 ScalaMockError01Spec.reportUnsatisfiedExpectation(ScalaMockError01Spec.scala:22) 在 org.scalamock.MockFactoryBase$class.verifyExpectations(MockFactoryBase.scala:108) 在 org.scalamock.MockFactoryBase$class.withExpectations(MockFactoryBase.scala:56) 在 ScalaMockError01Spec.withExpectations(ScalaMockError01Spec.scala:22) 在 org.scalamock.scalatest.AbstractMockFactory$class.withFixture(AbstractMockFactory.scala:34) at ScalaMockError01Spec.withFixture(ScalaMockError01Spec.scala:22) 在 org.scalatest.FlatSpecLike$class.invokeWithFixture$1(FlatSpecLike.scala:1679) 在 org.scalatest.FlatSpecLike$$anonfun$runTest$1.apply(FlatSpecLike.scala:1692) 在 org.scalatest.FlatSpecLike$$anonfun$runTest$1.apply(FlatSpecLike.scala:1692) at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289) at org.scalatest.FlatSpecLike$class.runTest(FlatSpecLike.scala:1692) at org.scalatest.FlatSpec.runTest(FlatSpec.scala:1685) at org.scalatest.FlatSpecLike$$anonfun$runTests$1.apply(FlatSpecLike.scala:1750) 在 org.scalatest.FlatSpecLike$$anonfun$runTests$1.apply(FlatSpecLike.scala:1750) 在 org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:396) 在 org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384) at scala.collection.immutable.List.foreach(List.scala:381) at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384) at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:373) 在 org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:410) 在 org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384) at scala.collection.immutable.List.foreach(List.scala:381) at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384) at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:379) at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461) at org.scalatest.FlatSpecLike$class.runTests(FlatSpecLike.scala:1750) at org.scalatest.FlatSpec.runTests(FlatSpec.scala:1685) at org.scalatest.Suite$class.run(Suite.scala:1147) at org.scalatest.FlatSpec.org$scalatest$FlatSpecLike$$super$run(FlatSpec.scala:1685) 在 org.scalatest.FlatSpecLike$$anonfun$run$1.apply(FlatSpecLike.scala:1795) 在 org.scalatest.FlatSpecLike$$anonfun$run$1.apply(FlatSpecLike.scala:1795) at org.scalatest.SuperEngine.runImpl(Engine.scala:521) at org.scalatest.FlatSpecLike$class.run(FlatSpecLike.scala:1795) at ScalaMockError01Spec.org$scalatest$BeforeAndAfterAll$$super$run(ScalaMockError01Spec.scala:22) 在 org.scalatest.BeforeAndAfterAll$class.liftedTree1$1(BeforeAndAfterAll.scala:213) 在 org.scalatest.BeforeAndAfterAll$class.run(BeforeAndAfterAll.scala:210) at ScalaMockError01Spec.run(ScalaMockError01Spec.scala:22) at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45) at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$1.apply(Runner.scala:1340) 在 org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$1.apply(Runner.scala:1334) at scala.collection.immutable.List.foreach(List.scala:381) at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1334) 在 org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1011) 在 org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1010) 在 org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1500) 在 org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1010) at org.scalatest.tools.Runner$.run(Runner.scala:850) at org.scalatest.tools.Runner.run(Runner.scala) at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:138) 在org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)
我可以知道为什么我会收到错误吗?
谢谢
愚蠢的我,我需要调用模拟函数
metricsRegistry.recordValue("my name is billa")