使用服务器示例代码进行测试不工作

Test With Server Example Code Not Working

我对 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]

什么是处理程序?是我的控制器吗?

我认为这是因为缺乏类型推断。

如果您添加所需的类型注释(即添加 : 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()
  }