找不到类型^的证据参数的隐含值

时间:2016-12-12 01:36:01

标签: scala spray-json

我正在尝试为Post请求编写测试

这是我的代码:

val request = CreateLinkRequest(token = Some(validToken),billing_ref_id = Some("123"), store_id = Some("123"), agent_id = Some("123"))

val endPoint = Uri(this.serverRootUrl + "path").toString
val post = Post(endPoint, request)
val pipeline = jsonAsStringPipeline(post)
val responseContents = Await.ready(pipeline, atMost = callMaxWaitDuration)

但是这并没有编译,我一直收到这个错误:

Error:(156, 20) could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[CreateLinkSpec.this.CreateLinkRequest]
    val post = Post(endPoint, request)
               ^
Error:(156, 20) not enough arguments for method apply: (implicit evidence$1: 

spray.httpx.marshalling.Marshaller[CreateLinkSpec.this.CreateLinkRequest])spray.http.HttpRequest in class RequestBuilder.
Unspecified value parameter evidence$1.
    val post = Post(endPoint, request)
                   ^

这是什么意思?

我该如何解决?

编辑: 这是身体中的json:

{ token:"123", billing_ref_id:"123", store_id:"123", agent_id:"123"}

这是代码中的对象:

private final case class CreateLinkRequest(
    token: Option[String] = Some(validToken),
    billing_ref_id: Option[String] = Some(Random.alphanumeric.take(10).mkString),
    agent_id: Option[String] = Some(Random.alphanumeric.take(3).mkString),
    store_id: Option[String] = Some(Random.alphanumeric.take(3).mkString)
    )

1 个答案:

答案 0 :(得分:6)

您正在尝试调用以Post为参数的implicit Marshaller方法。请注意,只要编译器可以在范围内找到一个参数,就不必提供隐式参数(请查看此信息以获取有关implicits的更多信息:Where does Scala look for implicits?

但是,您的代码没有定义任何隐式Marshaller,因此编译器不知道如何将case class转换为HttpEntity

在您的情况下,您希望将其转换为HttpEntity Content-Type: application/json。要做到这一点,您只需要定义:implicit val CreateLinkRequestMarshaller: Marshaller[CreateLinkRequest]。这告诉scala如何将case class转换为HttpEntity

您还希望将它作为JSON传递给上下文,因此我们将定义我们的JsonProtocol,即MyJsonProtocol

package test

import spray.http.HttpEntity
import spray.http._
import spray.json._
import spray.httpx.marshalling.{Marshaller, MarshallingContext}
import test.TestMarshaller.CreateLinkRequest


object MyJsonProtocol extends DefaultJsonProtocol {
  implicit def createLinkRequestFormat: JsonFormat[CreateLinkRequest] = jsonFormat4(CreateLinkRequest)
}

object TestMarshaller extends App {
  import MyJsonProtocol._

  case class CreateLinkRequest(token: Option[String], billing_ref_id: Option[String], store_id: Option[String], agent_id: Option[String])

  implicit val CreateLinkRequestMarshaller: Marshaller[CreateLinkRequest] = new Marshaller[CreateLinkRequest] {
    override def apply(value: CreateLinkRequest, ctx: MarshallingContext): Unit = {
      val entity = HttpEntity(MediaTypes.`application/json`, value.toJson.compactPrint)
      ctx.marshalTo(entity)
    }
  }

  // Here goes your test
}

请注意,您可以在其他位置定义这些含义,例如:一个package,然后只需在测试类中导入它。这样会更好,因为你肯定想重用Marshaller。