Scala &引用;不是合法的形式参数”;

Scala &引用;不是合法的形式参数”;,scala,intellij-idea,playframework,Scala,Intellij Idea,Playframework,我有一个单元测试: class MyServiceSpec extends WordSpec with Matchers with MockitoSugar with BeforeAndAfterEach { "MyService" must { "succeed if my-endpoint succeeds" in { Server.withRouter() { GET("/my-endpoint") => Action {

我有一个单元测试:

class MyServiceSpec extends WordSpec
  with Matchers
  with MockitoSugar
  with BeforeAndAfterEach {

  "MyService" must {
    "succeed if my-endpoint succeeds" in {
      Server.withRouter() {
        GET("/my-endpoint") => Action {
          Results.Ok.sendResource("myservice/my-endpoint.txt")
        }
      } { implicit port =>
        WsTestClient.withClient { client =>
          val result = Await.result(
            new RealMyService(client).getFromEndpoint(), 10.seconds)
          result shouldEqual true
        }
      }
    }
  }

}
sbt
告诉我:

» sbt test-only MyService
...
[error] /repos/myrepo/test/services/MyServiceSpec.scala:34: not a legal formal parameter.
[error] Note: Tuples cannot be directly destructured in method or function parameters.
[error]       Either create a single parameter accepting the Tuple1,
[error]       or consider a pattern matching anonymous function: `{ case (param1, param1) => ... }
[error]         GET("/my-endpoint") => Action {
[error]            ^
[error] one error found
[error] (test:compileIncremental) Compilation failed
[error] Total time: 3 s, completed Jun 16, 2017 7:27:11 AM
Application does not take parameters: } expected
IntelliJ告诉我:

» sbt test-only MyService
...
[error] /repos/myrepo/test/services/MyServiceSpec.scala:34: not a legal formal parameter.
[error] Note: Tuples cannot be directly destructured in method or function parameters.
[error]       Either create a single parameter accepting the Tuple1,
[error]       or consider a pattern matching anonymous function: `{ case (param1, param1) => ... }
[error]         GET("/my-endpoint") => Action {
[error]            ^
[error] one error found
[error] (test:compileIncremental) Compilation failed
[error] Total time: 3 s, completed Jun 16, 2017 7:27:11 AM
Application does not take parameters: } expected
在线:

GET("/my-endpoint") => Action {
这到底意味着什么?

服务器。withRouter()
需要一个模式匹配块。大概是这样的:

Server.withRouter() {
  case GET("/my-endpoint") => Action(whatever)
  case GET("/my-other-endpoint") => Action(whatever)
  case POST("/my-other-endpoint") => Action(whatever)
  case other => Action(whatever) // bad request
}
模式匹配只是一个局部函数,例如

whatever.map((i: Int) => i)

两者都做同样的事情。然而,最大的区别在于,第二种方法能够通过使用
unapply()
方法执行解构,这是模式匹配的全部要点


回到您的案例-模式匹配用于匹配
GET(“/myendpoint”)
case类(使用case类,您可以免费获得一些好东西,例如
unapply
自动为您定义)。如果没有模式匹配,你的块就没有意义;这将是一个正常的函数,其中左侧需要一个形式参数,例如
(i:Int)=>…
(s:String)=>…
。拥有
GET(“/my endpoint”)
根本没有意义,它不是一个正式的参数(这是SBT试图告诉您的)。

谢谢!我无意中删除了
case
关键字,从此再也没有注意到。虽然
sbt
IntelliJ
都不是很有帮助。我知道你的痛苦,但另一方面,sbt似乎对我很有帮助。您没有形式参数,而是要计算一个语句。这就像说无论什么(1+2)=>都是一个函数。但不管怎样,很高兴你把它解决了既然你提到了这一点,
sbt
就说到点子上了,但我的头脑顽固地拒绝了
sbt
提示中的
case
关键字!现在看来很明显。。。我应该学习一门课程“如何在不遗漏重要信息的情况下阅读错误信息”