【发布时间】:2017-10-17 14:39:25
【问题描述】:
我在玩 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.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) 在 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) 在 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) 在 org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:396) 在 org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384) 在 scala.collection.immutable.List.foreach(List.scala:381) 在 org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384) 在 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) 在 scala.collection.immutable.List.foreach(List.scala:381) 在 org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384) 在 org.scalatest.SuperEngine.org$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) 在 org.scalatest.FlatSpecLike$$anonfun$run$1.apply(FlatSpecLike.scala:1795) 在 org.scalatest.FlatSpecLike$$anonfun$run$1.apply(FlatSpecLike.scala:1795) 在 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) 在 org.scalatest.BeforeAndAfterAll$class.run(BeforeAndAfterAll.scala:210) 在 ScalaMockError01Spec.run(ScalaMockError01Spec.scala:22) 在 org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45) 在 org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$1.apply(Runner.scala:1340) 在 org.scalatest.tools.Runner$$anonfun$doRunRunDaDoRunRun$1.apply(Runner.scala:1334) 在 scala.collection.immutable.List.foreach(List.scala:381) 在 org.scalatest.tools.Runner$.doRunRunDaDoRunRun(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) 在 org.scalatest.tools.Runner$.run(Runner.scala:850) 在 org.scalatest.tools.Runner.run(Runner.scala) 在 org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:138) 在 org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)
我可以知道为什么会出现错误吗?
谢谢
【问题讨论】: