有一个"Testing with a server"示例,说明如何使用真正的HTTP堆栈测试Play框架应用程序。
我尝试使用Play 2.5.2和Scala 2.11.7编译示例但没有成功。最初的例子是没有进口。以下是我添加到get的导入的示例代码(几乎)编译:
package models
import org.scalatestplus.play.{OneServerPerSuite, PlaySpec}
import play.api.cache.EhCacheModule
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import play.api.routing.sird._
import play.api.routing._
import play.api.mvc._
import play.api.mvc.Results._
object TestExp04 {
}
class ExampleSpec extends PlaySpec with OneServerPerSuite {
// Override app if you need an Application with other than
// default parameters.
implicit override lazy val app =
new GuiceApplicationBuilder().disable[EhCacheModule].router(Router.from {
case GET(p"/") => Action { Ok("ok") }
}).build()
"test server logic" in {
val wsClient = app.injector.instanceOf[WSClient]
val myPublicAddress = s"localhost:$port"
val testPaymentGatewayURL = s"http://$myPublicAddress"
// The test payment gateway requires a callback to this server before it returns a result...
val callbackURL = s"http://$myPublicAddress/callback"
// await is from play.api.test.FutureAwaits
val response = await(wsClient.url(testPaymentGatewayURL).withQueryString("callbackURL" -> callbackURL).get())
response.status mustBe OK
}
}
最终的编译错误是:
[error] /home/js/workspace/example/server/test/models/TestExp04.scala:32: not found: value await
[error] val response = await(wsClient.url(testPaymentGatewayURL).withQueryString("callbackURL" -> callbackURL).get())
[error] ^
什么是神秘的await
?我应该导入什么才能让它运行?
作为对示例作者的反馈,我想说在准备示例时请不要剥离导入。有时可能需要花费大量时间来猜测进口的正确组合。
答案 0 :(得分:1)
实际上答案早于现有文件" Writing functional tests with ScalaTest"
建议导入所有助手:
import org.scalatest._
import org.scalatestplus.play._
import play.api.test._
import play.api.test.Helpers.{GET => GET_REQUEST, _}
这里的最后一次导入解决了这个问题。所以必要的进口是:
import play.api.test.Helpers.{GET => GET_REQUEST, _}
import org.scalatestplus.play.{OneServerPerSuite, PlaySpec}
import play.api.cache.EhCacheModule
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import play.api.routing.sird._
import play.api.routing._
import play.api.mvc._
import play.api.mvc.Results._
答案 1 :(得分:0)
导入play.api.test.FutureAwaits。
import play.api.test.FutureAwaits._
答案 2 :(得分:0)
或者您也可以使用,
scala.concurrent.Await.result(scala.concurrent.Future<actually it takes super class Awaitable>,
scala.concurrent.Duration)
如果花费的时间超过指定的持续时间,它会为您提供一种超时测试的好方法。