Pergunta

I'm trying to craft a specs2 test using Akka's TestKit. I'm stuck on a persistent compile error I can't figure out how to resolve, and I'd appreciate suggestions.

The compile error is:

TaskSpec.scala:40: parents of traits may not have parameters
[error]   with akka.testkit.TestKit( ActorSystem( "testsystem", ConfigFactory.parseString( TaskSpec.config ) ) )

Following suggestions from Akka docs and internet xebia and Akka in Action, I'm trying to incorporate the TestKit into a specs2 Scope. Here's a snippet of the code where I'm getting the error:

class TaskSpec 
extends Specification 
with AsyncTest
with NoTimeConversions { 

  sequential 

  trait scope 
  extends Scope 
  with TestKit( ActorSystem( "testsystem", ConfigFactory.parseString( TaskSpec.config ) ) ) 
  with AkkaTestSupport {
...

I have the following helper:

trait AkkaTestSupport extends After { outer: TestKit =>
  override protected def after: Unit = {
    system.shutdown()
    super.after
  }
}
Foi útil?

Solução

Here is one thing you can do:

import org.specs2.mutable.SpecificationLike
import org.specs2.specification._

class TestSpec extends Actors { isolated
  "test1" >> ok
  "test2" >> ok
}

abstract class Actors extends 
 TestKit(ActorSystem("testsystem", ConfigFactory.parseString(TaskSpec.config)))
 with SpecificationLike with AfterExample {

  override def map(fs: =>Fragments) = super.map(fs) ^ step(system.shutdown, global = true)

  def after = system.shutdown
}

This should avoid the compilation error you had because TestKit is an abstract class and it is only mixing-in traits: SpecificationLike is a trait (Specification isn't) and AfterExample is a trait.

Also the specification above runs in the isolated mode, meaning that there is a brand new TestSpec object instantiated for each example and the AfterExample trait makes sure that the system is shutdown after each example.

Finally the map method is overriden with a special step to make sure that the system created for the first TestSpec instance (the one declaring all the examples) will be cleanly disposed of.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top