0
votes

I was playing with ScalaMock. I wrote a KISS version of the error of I facing when I am trying to use ScalaMock to mock an object in a test case.

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"
    }
  }
}

The error I am getting is as follows

Unsatisfied expectation:

Expected: inAnyOrder { inAnyOrder { MetricsRegistry.recordValue(*) twice (never called - UNSATISFIED) } }

Actual: ScalaTestFailureLocation: ScalaMockError01Spec at (ScalaMockError01Spec.scala:22) org.scalatest.exceptions.TestFailedException: Unsatisfied expectation:

Expected: inAnyOrder { inAnyOrder { MetricsRegistry.recordValue(*) twice (never called - UNSATISFIED) } }

Actual: at org.scalamock.scalatest.AbstractMockFactory$class.newExpectationException(AbstractMockFactory.scala:51) at ScalaMockError01Spec.newExpectationException(ScalaMockError01Spec.scala:22) at ScalaMockError01Spec.newExpectationException(ScalaMockError01Spec.scala:22) at org.scalamock.context.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) at ScalaMockError01Spec.withFixture(ScalaMockError01Spec.scala:22) at org.scalatest.FlatSpecLike$class.invokeWithFixture$1(FlatSpecLike.scala:1679) at org.scalatest.FlatSpecLike$$anonfun$runTest$1.apply(FlatSpecLike.scala:1692) at 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) at 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) at org.scalatest.Suite$class.run(Suite.scala:1147) at org.scalatest.FlatSpec.org$scalatest$FlatSpecLike$$super$run(FlatSpec.scala:1685) at 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) at org.scalatest.FlatSpecLike$class.run(FlatSpecLike.scala:1795) at ScalaMockError01Spec.org$scalatest$BeforeAndAfterAll$$super$run(ScalaMockError01Spec.scala:22) at 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) at 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 org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1334) at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1011) at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1010) at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1500) at 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) at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)

May I know why I am getting the error?

Thanks

1

1 Answers

0
votes

silly me, I need to call the mocked function

metricsRegistry.recordValue("my name is billa")