scalamock newexpectationexception - [scala,scalamock,akka-http]

我在玩ScalaMock . 当我尝试使用ScalaMock来模拟测试用例中的对象时,我写了一个KISS版本的错误 .

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()两次(从不调用 - UNSATISFIED)}}实际:ScalaTestFailureLocation:ScalaMockError01Spec at(ScalaMockError01Spec.scala:22)org.scalatest.exceptions.TestFailedException:不满意的期望:预期:inAnyOrder {inAnyOrder {MetricsRegistry.recordValue()两次(从未调用过 - UNSATISFIED)}}实际:at org.scalamock.scalatest.AbstractMockFactory $ class.newExpectationException(AbstractMockFactory.scala:51)at ScalaMockError01Spec.newExpectationException(ScalaMockError01Spec.scala: 22)在org.scalamock.MockFactoryBase $的scalaMockError01Spec.newExpectationException(ScalaMockError01Spec.scala:22)org.scalamock.context.MockContext $ class.reportUnsatisfiedExpectation(MockContext.scala:45)at ScalaMockError01Spec.reportUnsatisfiedExpectation(ScalaMockError01Spec.scala:22)at org.scalamock.MockFactoryBase $ org.scalamock.MockFactoryBase $ class.withExpectations(MockFact)中的class.verifyExpectations(MockFactoryBase.scala:108) oryBase.scala:56)ScalaMockError01Spec.withExpectations(ScalaMockError01Spec.scala:22)at org.scalamock.scalatest.AbstractMockFactory $ class.withFixture(AbstractMockFactory.scala:34)at SclaMockError01Spec.withFixture(ScalaMockError01Spec.scala:22)at org . scalatest.FlatSpecLike $ class.invokeWithFixture $ 1(FlatSpecLike.scala:1679)org.scalatest.FlatSpecLike $$ anonfun $ runTest $ 1.apply(FlatSpecLike.scala:1692)at org.scalatest.FlatSpecLike $$ anonfun $ runTest $ 1.apply (FlatSpecLike.scala:1692)在org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)在org.scalatest.FlatSpecLike $ class.runTest(FlatSpecLike.scala:1692)在org.scalatest.FlatSpec.runTest(FlatSpec . 斯卡拉:1685)在org.scalatest.FlatSpecLike $$ anonfun $ runTests $ 1.适用(FlatSpecLike.scala:1750)在org.scalatest.FlatSpecLike $$ anonfun $ runTests $ 1.适用(FlatSpecLike.scala:1750)在org.scalatest .SuperEngine $$ anonfun $ traverseSubNodes $ 1 $ 1.apply(Engine.scala:396)at org.scalatest.SuperEngine $$ anonfun $ traverseSubNodes $ 1 $ 1.apply(Engine.scala:3 84)在org.scalatest.SuperEngine.traverseSubNodes的scala.collection.immutable.List.foreach(List.scala:381)org.scalatest.SuperEngine.org $ 1(Engine.scala:384)$ scalatest $ SuperEngine $$ runTestsInBranch (Engine.scala:373)org.scalatest.SuperEngine $$ anonfun $ traverseSubNodes $ 1 $ 1.apply(Engine.scala:410)at org.scalatest.SuperEngine $$ anonfun $ traverseSubNodes $ 1 $ 1.apply(Engine.scala:384) )在org.scalatest.SuperEngine.traverseSubNodes的scala.collection.immutable.List.foreach(List.scala:381)org.scalatest.SuperEngine.org $ 1(Engine.scala:384)$ scalatest $ SuperEngine $$ runTestsInBranch( Engine.scala:379)在org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461)在org.scalatest.FlatSpecLike $ class.runTests(FlatSpecLike.scala:1750)在org.scalatest.FlatSpec.runTests(FlatSpec.scala :1685)org.scalatest.Suite $ class.run(Suite.scala:1147)org.scalatest.FlatSpec.org $ scalatest $ FlatSpecLike $$ super $ run(FlatSpec.scala:1685)at org.scalatest.FlatSpecLike $$ anonfun $运行$ 1.适用(FlatSpecLike.scala:1795)在org.scalatest.FlatSpecLike $$ anonfun $运行$ 1.适用(FlatSpecLike.scala:1795)在org.scalatest.SuperEngine.runImpl(Engine.scala:521)在org.scalatest.FlatSpecLike $ class.run(FlatSpecLike.scala :在org.scalatest.BeforeAndAfterAll $ class.liftedTree1 $ 1(BeforeAndAfterAll.scala 22):1795)在ScalaMockError01Spec.org $ scalatest $ BeforeAndAfterAll $$超$运行(ScalaMockError01Spec.scala 213)在org.scalatest.BeforeAndAfterAll $类 . 在org.scalatest.tool.SuiteRunner.run(SuiteRunner.scala:45)的scalaMockError01Spec.run(ScalaMockError01Spec.scala:22)上运行(BeforeAndAfterAll.scala:210)org.scalatest.tools.Runner $$ anonfun $ doRunRunRunDaDoRunRun $ 1 .apply(Runner.scala:1340)at org.scalatest.tools.Runner $$ anonfun $ doRunRunRunDaDoRunRun $ 1.apply(Runner.scala:1334)at scala.collection.immutable.List.foreach(List.scala:381)at at org.scalatest.tools.Runner $ .doRunRunRunDaDoRunRun(Runner.scala:1334)在org.scalatest.tools.Runner $$ anonfun $ runOptionallyWithPassFailReporter $ 2.适用(Runner.scala:1011)在org.scalatest.tool s.Runner $$ anonfun $ runOptionallyWithPassFailReporter $ 2.apply(Runner.scala:1010)at org.scalatest.tools.Runner $ .withClassLoaderAndDispatchReporter(Runner.scala:1500)at atorg.scalatest.tools.Runner $ .runOptionallyWithPassFailReporter(Runner.scala:1010)org.scalatest.tools.Runner $ .run(Runner.scala:850)at org.scalatest.tools.Runner.run(Runner.scala) org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:138)at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)

我可以知道为什么会收到错误吗?

谢谢

回答(1)

2 years ago

傻我,我需要调用模拟函数

metricsRegistry.recordValue(“我的名字是billa”)