Using Akka TestKit with Specs2

I am trying to create a specs2 test using Akka TestKit. I am stuck in a constant compilation error, I cannot figure out how to solve it, and I will be grateful for the suggestions.

Compilation Error:

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

The following offers Akka and the Internet xebia and Akka in action, I am trying to include TestKit in the scope of specs2. Here is the code snippet where I get the error message:

 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 } } 
+6
source share
1 answer

Here is what 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 a compilation error, because TestKit is an abstract class, and these are just signs of confusion: SpecificationLike is a sign ( Specification is not) and AfterExample is a sign.

Also, the above specification works in isolated mode, which means that a new TestSpec object is created for each example, and the AfterExample ensures that the system will be disconnected after each example.

Finally, the map method is overridden by a special step to make sure that the system created for the first TestSpec instance (the one that declares all the examples) will be cleared.

+6
source

Source: https://habr.com/ru/post/957866/


All Articles