The execution of a Specification depends on various parts, among which:
ExecutorService
for concurrent executionAll of this is bundled into one object org.specs2.specification.core.Env
. The Env
is accessible to your Specification by either:
The following objects can be injected in your specification if you declare a 1-parameter constructor:
Env
itselfArguments
objectCommandLine
objectExecutionEnv
object (can be implicit)ExecutionContext
object (can be implicit)ExecutorService
object (can be implicit)For example:
class MySpec(env: Env) extends Specification { def is =
s2"""
Use the environment fileSystem
${ env.fileSystem.mkdirs("tmp" / "test").runOption; ok }
"""
}
Or if you want to access an ExecutionContext
:
class MySpec(implicit ec: ExecutionContext) extends Specification { def is =
s2"""
Use a future
${ Await.result(Future(1), 1.seconds) must_== 1 }
"""
}
The Env
object can also be accessed by mixing-in the org.specs2.specification.Environment
trait
class MySpec extends Specification with Environment { def is(env: Env) =
s2"""
Use the environment fileSystem
${ env.fileSystem.mkdirs("tmp" / "test").runOption; ok }
"""
}
As you can see, instead of defining the is
method you now need to defined the is(env: Env)
method. Then you can access any attribute of the current Env
. There are also other specialised traits giving access to specific parts of the environment
When you just want to access the command-line arguments you can use the org.specs2.specification.CommandLineArguments
trait
class MySpec extends Specification with CommandLineArguments { def is(args: CommandLine) = s2"""
Use the command line arguments
${ if (args.isSet("pass")) ok else ko }
"""
}
When you just want to access the execution environment can use the org.specs2.specification.ExecutionEnvironment
trait
class MySpec extends Specification with ExecutionEnvironment { def is(implicit ee: ExecutionEnv) = s2"""
Use the implicit execution environment
${ Future(1) must be_==(1).await }
"""
}
As you can see the ExecutionEnv
parameter is defined as an implicit parameter because this is what is required when creating futures or using Future matchers.