0

我在玩 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(*) 两次(从未调用 - 不满意)} }

实际:ScalaTestFailureLocation:ScalaMockError01Spec at (ScalaMockError01Spec.scala:22) org.scalatest.exceptions.TestFailedException:不满意的期望:

预期:inAnyOrder { inAnyOrder { MetricsRegistry.recordValue(*) 两次(从未调用 - 不满意)} }

实际:在 org.scalamock.context 的 ScalaMockError01Spec.newExpectationException(ScalaMockError01Spec.scala:22) 的 ScalaMockError01Spec.newExpectationException(ScalaMockError01Spec.scala:22) 的 org.scalamock.scalatest.AbstractMockFactory$class.newExpectationException(AbstractMockFactory.scala:51)。 MockContext$class.reportUnsatisfiedExpectation(MockContext.scala:45) at ScalaMockError01Spec.reportUnsatisfiedExpectation(ScalaMockError01Spec.scala:22) at org.scalamock.MockFactoryBase$class.verifyExpectations(MockFactoryBase.scala:108) at org.scalamock.MockFactoryBase$class.withExpectations (MockFactoryBase.scala:56) at ScalaMockError01Spec.withExpectations(ScalaMockError01Spec.scala:22) at org.scalamock.scalatest.AbstractMockFactory$class.withFixture(AbstractMockFactory.scala:34) 在 org.scalatest.FlatSpecLike$class.invokeWithFixture$1(FlatSpecLike.scala:1679) 在 org.scalatest.FlatSpecLike$$anonfun$runTest$1.apply(FlatSpecLike.scala: 的 ScalaMockError01Spec.withFixture(ScalaMockError01Spec.scala:22): 1692) 在 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.scala:1685) 在 org.scalatest.FlatSpecLike$$anonfun$runTests$1.apply(FlatSpecLike.scala:1750) 在 org.scalatest.FlatSpecLike$$anonfun $runTests$1.apply(FlatSpecLike.scala:1750) at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:396) at 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) at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:410) at 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) 在 org.scalatest.Suite$class.run(Suite.scala:1147) 在 org.scalatest.FlatSpec.org$scalatest$FlatSpecLike$$super$run(FlatSpec.scala:1685) 在 org.scalatest.FlatSpecLike$ $anonfun$run$1.apply(FlatSpecLike.scala:1795) at org.scalatest.FlatSpecLike$$anonfun$run$1.apply(FlatSpecLike.scala:1795) at org.scalatest.SuperEngine.runImpl(Engine.scala:521)在 org.scalatest.FlatSpecLike$class.run(FlatSpecLike.scala:1795) 在 ScalaMockError01Spec.org$scalatest$BeforeAndAfterAll$$super$run(ScalaMockError01Spec.scala:22) 在 org.scalatest.BeforeAndAfterAll$class.liftedTree1$1(BeforeAndAfterAll .scala:213) at org.scalatest.BeforeAndAfterAll$class.run(BeforeAndAfterAll.scala:210) at ScalaMockError01Spec.run(ScalaMockError01Spec.scala:22) at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45)在组织。scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRun$1.apply(Runner.scala:1340) at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRun$1.apply(Runner.scala:1334) at scala.collection.immutable. List.foreach(List.scala:381) at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1334) at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1011 ) 在 org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1500) 在 org.scalatest.tools.Runner 的 org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1010) $.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。斯卡拉。testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:138) 在 org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)

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

谢谢

4

1 回答 1

0

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

metricsRegistry.recordValue("我叫比拉")

于 2017-05-18T00:16:37.080 回答