Search code examples
scalaakkaspecs2

Testing Akka with Specs2


I have the following specs2 test:

package concurrent

import akka.actor.{Props, actorRef2Scala}
import akka.testkit.TestActorRef

import scala.concurrent.duration._

class MessageCoordinatingActorSpec extends ActorBaseSpec {

  "MessageCoordinatingActor" should {
    "receive a Result and update the related token and status" in {
      val (repositoryActorProbe, messageCoordinatingActorRef) = buildMockMessageCoordinatingActor


      val addresses = MockSession.getTestAddressesWithServicesAt(
                                   "16 Main Street", List("Service1", "Service2"))
      val postCode = Postcode("NE28 9QR", addresses)

      val matchedAddr = addresses.find(_.addrToken=="16 Main Street")


      messageCoordinatingActorRef ! Result(LookupResult(
        LookupStatus.ServicesAvailable, postCode, matchedAddr, true), testRecord)

      //Expect message to persist the virgin postcode record
      repositoryActorProbe.expectMsg(pairLongToDuration(3, SECONDS),
        PersistPostcode(postCode))

      1 mustEqual 1
    }
  }
}

abstract class ActorBaseSpec extends TestKit(ActorSystem("test")) with ImplicitSender with MustMatchers with SpecificationLike with Mockito {
    //This class just contains some fixture factory methods 
    //such as buildMockMessageCoordinatingActor
}

The content of the test is pretty unimportant to my question, but i have two problems. Firstly notice i had to use

pairLongToDuration(3, SECONDS)

I want to be able to use

3.seconds (from the scala.concurrent.duration package)

but when i do, i get the following error:

[error]  found   : org.specs2.time.Duration
[error]  required: scala.concurrent.duration.FiniteDuration

Any idea how i can get around this?

Also notice i had to stick

1 mustEqual 1

at the bottom. If i take this out i get

[error] /Users/.../MessageCoordinatingActorSpec.scala:18: could not find implicit value for evidence parameter of type org.specs2.execute.AsResult[concurrent.RepositoryActor.UpdateAddrToken]

It seems that specs2 does not recognise the probe.expectMsg success as being a test success, is there any way to fix this in a more satisfactory way?

Cheers! NFV


Solution

  • You need to mix in the org.specs2.time.NoTimeConversions trait for the duration issue.

    Then you can create an implicit instance for the AsResult[UpdateAddrToken] typeclass:

    implicit def tokenAsResult = new AsResult[UpdateAddrToken] {
      def asResult(r: =>UpdateAddrToken) = success
    }