簡體   English   中英

找不到類型^的證據參數的隱含值

[英]could not find implicit value for evidence parameter of type ^

我正在嘗試為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)
    )

您正在嘗試調用Post方法,該方法將implicit Marshaller作為參數。 請注意,只要編譯器可以在作用域中找到一個隱式參數,就不必提供隱式參數(有關implicits的更多信息,請查看此參數: Scala在哪里查找implicits?

但是,您的代碼沒有定義任何隱式Marshaller,因此編譯器不知道如何將您的case class轉換為HttpEntity

在您的情況下,您希望將其轉換為帶有Content-Type: application/jsonHttpEntity 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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM