如何组合WithApplication(Loader)和ExecutionEnv

时间:2016-10-31 22:45:51

标签: scala intellij-idea playframework specs2

我正在为使用Scala和Play框架在项目中返回期货的方法编写Specs2测试。 Documentationthis question的答案建议使用await修饰符,这需要添加隐式ExecutionEnv。一个最小的工作示例(改编自上述答案之一):

class FutureSpec extends mutable.Specification {
  "Even in future one" should {
    "be greater than zero" in { implicit ee: ExecutionEnv =>
      Future(1) must be_>(0).await
    }
  }
}

但我的一些测试需要WithApplicationLoader。如果我将它添加到示例中,则无法编译:

class FutureSpec extends mutable.Specification {
  "Even in future one" should {
    "be greater than zero" in new WithApplicationLoader { implicit ee: ExecutionEnv =>
      Future(1) must be_>(0).await
    }
  }
}

WithApplication而不是WithApplicationLoader具有完全相同的效果(预期)。

是否可以将WithApplicationLoader与隐式ExecutionEnv一起使用?

不幸的是,文档中描述的第二个选项 - 将ExecutionEnv移动到类构造函数而不是特定方法 - 不可用。本规范:

class FutureSpec(implicit ee: ExecutionEnv) extends mutable.Specification {
  "Even in future one" should {
    "be greater than zero" in new WithApplicationLoader {
      Future(1) must be_>(0).await
    }
  }
}

可以工作,但IntelliJ Idea会忽略它(我可以单独运行这样的规范,但是在项目中运行所有测试的配置都不会执行它。)

2 个答案:

答案 0 :(得分:1)

这适用于IntelliJ 2016.1.3:

import play.api.test.{PlaySpecification, WithApplication}
import org.specs2.concurrent.ExecutionEnv
import scala.concurrent.Future

class FutureSpec extends PlaySpecification {
  implicit val ee = ExecutionEnv.fromGlobalExecutionContext
//  // or this:
//  implicit val ee = ExecutionEnv.fromExecutionContext(play.api.libs.concurrent.Execution.Implicits.defaultContext)

  "Even in future one" should {
    "be greater than zero" in new WithApplication {
      Future(1) must be_>(0).await
    }
  }
}

这是我的build.sbt

name := "throwaway"

version := "1.0"

scalaVersion := "2.11.8"

libraryDependencies += "org.specs2" %% "specs2-core" % "3.8.5.1" % "test"

libraryDependencies += "com.typesafe.play" %% "play" % "2.5.9" % "test"

libraryDependencies += "com.typesafe.play" %% "play-specs2" % "2.5.9" % "test"

libraryDependencies += "com.typesafe.play" %% "play-test" % "2.5.9" % "test"

答案 1 :(得分:0)

你可以随时这样做

class TestSpec extends mutable.Specification {
  "test1" >> { implicit ee: ExecutionEnv =>
    new WithApplicationLoader {
      ok
    }
  }
}