Scala 如何修复测试规范中参数ta:TildeArrow缺少的隐式值

Scala 如何修复测试规范中参数ta:TildeArrow缺少的隐式值,scala,routing,spray,scala-2.9,spray-test,Scala,Routing,Spray,Scala 2.9,Spray Test,我正在使用spray编写一个简单的测试规范,但我无法正确编译它,不知道我是否做错了什么。我的scala版本是2.9.3和spray 1.0.1(更新这两个版本都不是合适的选项)。以下是我的测试规范代码: import org.specs2.mutable.Specification import spray.testkit.Specs2RouteTest import spray.http._ import akka.util.Duration import java.util.concurre

我正在使用spray编写一个简单的测试规范,但我无法正确编译它,不知道我是否做错了什么。我的scala版本是2.9.3和spray 1.0.1(更新这两个版本都不是合适的选项)。以下是我的测试规范代码:

import org.specs2.mutable.Specification
import spray.testkit.Specs2RouteTest
import spray.http._
import akka.util.Duration
import java.util.concurrent.TimeUnit

import service.MyProxy

abstract class MyTestSpec extends Specification with Specs2RouteTest with MyProxy{

  val duration = Duration(30, TimeUnit.SECONDS)
  implicit val routeTestTimeout = RouteTestTimeout(duration)

  "MyProxy" should {

    "return a json for GET requests to the /api/getclass/classCode path for a regular request" in {
      Get("/api/getclass/123/") ~> myRoutes~> check {
        responseAs[String] must contain("classCode")
        contentType === ContentTypes.`application/json`
      }
    }

  } // end should...
} //end class
我在运行测试时遇到此错误

[error] C:\Users\Desktop\Project\MyTestSpec.scala:23: could not find implicit value for parameter ta: MyProxySpec.this.TildeArrow[spray.routing.RequestContext,Unit]
[error]       Get("/api/getclass/123/") ~> myRoutes~> check {
[error]                                         ^
[error] one error found
[error] (test:compile) Compilation failed
我尝试过其他问题的不同解决方案,到目前为止似乎没有任何效果


注意:为了记录在案,我没有在这个问题上使用scalatest或scalacheck。这纯粹是一个[spray]路由测试。如果
myRoutes
实际上不是一个路由,而是一个
指令[HNil]
,我可以用Scala 2.10重现完全相同的错误消息

因此,我猜测在unshown service.MyProxy类中,您的路由没有完成

给出了这个错误

trait MyProxy extends HttpService {
  val myRoutes = path("foo") {
   complete(StatusCodes.Accepted)
 }
}

没有。

ScalatestRouteTest已经提供了一个隐式的ActorySystem。从ActorRefactory方法中删除“implicit”修饰符,测试将被执行


我只是在努力解决这个问题。为了弄清楚这一点,我涉过了akkahttpcodebase,这是一个
隐式
的丛林

我的问题似乎是,如果没有正确的第二个
隐式
,就找不到正确的
TildeArrow
实例。如果查看代码,则错误消息中需要的
TildeArrow
实例被定义为伴随对象中的,并且它需要大量其他
隐式
s

我建议您在编写代码时不要使用任何隐含的糖分。这将更好地帮助编译器提供正确的错误消息:

"MyProxy" should {
  "return a json for GET requests to the /api/getclass/classCode path for a regular request" in {
    val request = Get("/api/getclass/123/")
    val requestWithRoutes = request.~>(myRoutes)(TildeArrow.injectIntoRoute)
    requestWithRoutes.~>(check {
      responseAs[String] must contain("classCode")
      contentType === ContentTypes.`application/json`
    })
  }
}
我认为原因是因为没有可用的
隐式
的具体实例,编译器试图用
抽象类TildeArrow
来满足隐式解析,而完全未指定的抽象
类型
ta.Out
,没有定义
~>

在我的具体案例中,我缺少了一个隐式的
ExecutionContextExecutor
,不管这意味着什么


更新

事实上,我进一步研究了它,问题是在我的route trait中声明了一个
隐式def ec:ExecutionContextExecutor
,但将其名称定义为
executor
,因此当我定义了我自己的以实现缺少的隐式时,我得到了两个相同的隐式

这是一个可爱的API,但隐式的魔力是无法控制的,尤其是考虑到错误消息往往是多么神秘。

对于akka http:

在我的情况下,哪一个是指

  • executionContext
    隐式
    修饰符,也需要删除
  • 并且应该像这样对特性进行重新排序:
    classservicespec使用带有ScalatestRouteTest的服务的匹配器扩展FlatSpec

在我的例子中,当我在测试用例中也导入了
import monix.execution.Scheduler.Implicits.global
(可能有某种
ExecutionContext
)时,出现了找不到的隐式错误


我修复了在我需要的方法中添加monix调度程序导入的问题,而不是在顶部导入中。

您的build.sbt应该对akka流测试和akka测试具有依赖关系

然后,它应该得到所有依赖项

请参阅本文件:-


要对前面给出的答案进行一点扩展,有一些隐式不应该由测试类扩展的任何其他特性或类声明:

  • Actor系统
  • 行刑语境
  • DefaultHostInfo(包akka.http.scaladsl.testkit)
  • 催化剂
如果在
ScalatestRouteTest
之外的其他位置声明了其中任何一项,则会引发以下错误:

could not find implicit value for parameter ...TildeArrow[RequestContext, SomethingHereOther]

这对我很有帮助,对我来说也是这样。
could not find implicit value for parameter ...TildeArrow[RequestContext, SomethingHereOther]