2016-12-12 10 views
2

^タイプの証拠パラメータの暗黙の値を見つけることができなかった私のコードは次のとおりです。は、私はPOSTリクエストここ</p> <p>のためのテストを記述しようとしています

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) 

しかしこのdoesntのコンパイル、私はこれを得続けますエラー:

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) 
       ^

これはどういう意味ですか?

どうすれば修正できますか?

EDIT: これは体内で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) 
    ) 
+4

[marshallerパラメータの暗黙的な値が見つかりませんでした:sp​​ray.httpx.marshalling.ToResponseMarshaller](http://stackoverflow.com/questions/20408734/could-not-find-implicit-value-for-parameter -marshaller-spray-httpx-marshalling) – engineerC

+0

残念ながらこれは私の問題を解決しません –

+0

あなたのPOSTルートはどのように見えますか? JSONが本体に必要ですか? –

答えて

3

あなたはimplicit Marshallerを取るPostメソッドを呼び出すようにしようとしていますパラメータとして。

しかし、あなたのコードは、任意の暗黙のマーシャラーがそのように定義されていません。暗黙のパラメータがある限り、コンパイラは、スコープ内の1は、(Where does Scala look for implicits?暗黙についての詳細はこのことを確認してください)見つけることができるように提供する必要がないことに注意してくださいコンパイラはcase classHttpEntityに変換する方法を知りません。

HttpEntityに変換する場合は、Content-Type: application/jsonです。これを行うには、定義する必要があります:implicit val CreateLinkRequestMarshaller: Marshaller[CreateLinkRequest]。これは、case classHttpEntityに変換する方法をスカラーに伝えます。

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 
} 

これらのインプリシットを他の場所で定義することができます。 a packageして、テストクラスにインポートしてください。これは、あなたが確かにマーシャルを再利用したいからです。

関連する問題