如何在测试规范中修复参数ta缺少的隐式值:TildeArrow [英] How can I fix the missing implicit value for parameter ta: TildeArrow in a test spec

查看:121
本文介绍了如何在测试规范中修复参数ta缺少的隐式值:TildeArrow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spray开发一个简单的测试规范,但无法正确编译,也不知道我做错了什么。我的scala版本是2.9.3,喷涂版本1.0.1(更新这两个都不是合适的选择)。这是我的测试规范的代码:

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

导入服务.MyProxy

抽象类MyTestSpec通过Specs2RouteTest和MyProxy {扩展了Specs2RouteTest的规范{

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

MyProxy应该{{

将GET请求的json返回到常规请求的/ api / getclass / classCode路径, {{
Get( / api / getclass / 123 / )〜> myRoutes〜>检查{
responseAs [String]必须包含( classCode)
contentType ===ContentTypes。`application/ json`
}
}

} //结束应该...
} //结束类

运行测试时会出现此错误。

  [错误] C:\Users\Desktop\Project\MyTestSpec。 scala:23:找不到参数ta的隐含值:MyProxySpec.this.TildeArrow [spray.routing.RequestContext,Unit] 
[错误] Get( / api / getclass / 123 /)〜> myRoutes〜>检查{
[错误] ^
[错误]发现一个错误
[错误](测试:编译)编译失败

我尝试了在另一个问题上看到的不同解决方案,到目前为止似乎没有任何效果。



Spray.io:无法编译测试规范



如何使用Spraytestkit和HttpServiceActor使scalatest工作



使用基本Spray-Testkit来测试路由不起作用



https://groups.google.com/forum/#!topic/spray-user/H5hkXuDGWYQ



https://groups.google.com/论坛/#!topic / spray-user / zFUJSVBPM5c



注意:出于记录,我没有使用scalatest或对此进行scalacheck。纯粹是一个[spray]路由测试。MyProxy扩展了Actor

解决方案

我只是在解决这个问题。为了弄清楚,我仔细阅读了 Akka HTTP代码库,它是隐式的丛林。



我的问题似乎是,如果没有正确的次要隐式,正确的找不到TildeArrow 实例。如果您查看代码,则错误消息中必需的 TildeArrow 实例被定义为 隐含定义伴随对象中的injectIntoRoute ,它需要一整堆其他隐式



我建议写出没有任何隐含糖的代码。这应该更好地帮助编译器为您提供正确的错误消息:

  MyProxy应该{
返回json在{
val request = Get( / api / getclass / 123 /)
val requestWithRoutes = request中,将常规请求的GET请求发送到/ api / getclass / classCode路径。 (myRoutes)(TildeArrow.injectIntoRoute)
requestWithRoutes。〜>(检查{
responseAs [String]必须包含( classCode)
contentType ===ContentTypes。application/ json。
})
}
}

我认为原因是因为没有可用的 implicit 的具体实例,所以编译器试图使用抽象类TildeArrow ,而完全未指定的抽象类型 ta.Out ,则没有 〜> 定义。



在我的特定情况下,无论什么意思,我都缺少一个隐式的 ExecutionContextExecutor






更新



实际上,我进一步调查了一下,问题是我在路由特征中声明了隐式def ec:ExecutionContextExecutor ,但是 trait RouteTest 将其名称定义为 executor ,因此,当我定义自己的名称以实现缺少的隐式时,我最终得到了



这是一个可爱的API,但是IMO却无法控制隐式魔术,特别是考虑到错误消息的隐秘性。 / p>

I'm working on a simple test spec using spray and I can't get it to compile correctly, don't know if I'm doing anything wrong. My version of scala is 2.9.3 and spray 1.0.1 (Updating either of them is not a suitable option). Here's my test spec's code:

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

I'm getting this error when I run the test.

[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

I've tried different solutions seen on another questions and nothing seems to work so far.

Spray.io: Can't compile test spec

how to make scalatest work with spraytestkit and HttpServiceActor

Basic Spray-Testkit usage to test a route does not work

https://groups.google.com/forum/#!topic/spray-user/H5hkXuDGWYQ

https://groups.google.com/forum/#!topic/spray-user/zFUJSVBPM5c

NOTE: Just for the record, I'm not using scalatest or scalacheck on this. Is purely a [spray] route test.And MyProxy extends Actor

解决方案

I just struggled with this problem. To figure it out, I waded through the Akka HTTP codebase, which is a jungle of implicits.

My problem seemed to be that without the right secondary implicit in place, the correct TildeArrow instance wasn't being found. If you look at the code, the TildeArrow instance, which is required in the error message, is defined as an implicit def injectIntoRoute in the companion object and it requires a whole host of other implicits.

I suggest writing out your code without any of the implicit sugar. This should better help the compiler give you a proper error message:

"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`
    })
  }
}

I think the reason is that since there's no concrete instance of the implicit available, the compiler is trying to satisfy the implicit resolution with the abstract class TildeArrow, and the completely unspecified abstract type, ta.Out, doesn't have a ~> defined.

In my specific case, I was missing an implicit ExecutionContextExecutor, whatever that means.


UPDATE

Actually, I looked into it further and the problem was that I had an implicit def ec: ExecutionContextExecutor declared in my route trait, but trait RouteTest defines its name as executor, and so when I defined my own to fulfill the missing implicit, I ended up with two of the same implicit.

It's a cute API, but the implicit magic is way out of control, IMO, especially given how cryptic the error messages tend to be.

这篇关于如何在测试规范中修复参数ta缺少的隐式值:TildeArrow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆