测试服务器示例代码不起作用

问题描述 投票:0回答:1

我对Play 2.7 documentation中的这个例子并不了解

class ExampleSpec extends PlaySpec with GuiceOneServerPerSuite {

  // Override app if you need an Application with other than
  // default parameters.
  override def fakeApplication(): Application = {
    GuiceApplicationBuilder()
      .appRoutes(app => {
        case ("GET", "/") => app.injector.instanceOf(classOf[DefaultActionBuilder]) { 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).addQueryStringParameters("callbackURL" -> callbackURL).get())

    response.status mustBe OK
  }
}

问题是这段代码:

  .appRoutes(app => {
    case ("GET", "/") => app.injector.instanceOf(classOf[DefaultActionBuilder]) { Ok("ok") }

我收到消息,它期望Application => PartialFunction [(String,String),Handler]

什么是汉德勒?是我的控制员吗?

scala playframework
1个回答
1
投票

这归结为我认为缺乏类型推断。

如果添加所需的类型注释(即添加: PartialFunction[(String, String), Handler]),您应该能够编译:

  override def fakeApplication(): Application = {
    GuiceApplicationBuilder()
      .appRoutes(app => {
        case ("GET", "/") => app.injector.instanceOf(classOf[DefaultActionBuilder]) { x => play.api.mvc.Results.Forbidden }
      }: PartialFunction[(String, String), Handler]
      ).build()
  }
© www.soinside.com 2019 - 2024. All rights reserved.