我正在尝试为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)
Run Code Online (Sandbox Code Playgroud)
但这不编译,我不断收到此错误:
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)
^
Run Code Online (Sandbox Code Playgroud)
这是什么意思?
我该如何解决?
编辑:这是身体中的json:
{ token:"123", billing_ref_id:"123", store_id:"123", agent_id:"123"}
Run Code Online (Sandbox Code Playgroud)
这是代码中的对象:
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)
)
Run Code Online (Sandbox Code Playgroud)
您正在尝试调用Post以implicit Marshaller参数为参数的方法.请注意,只要编译器可以在作用域中找到一个隐式参数,就不必提供隐式参数(请查看此内容以获取有关implicits的更多信息:Scala在哪里查找implicits?)
但是,您的代码没有定义任何隐式Marshaller,因此编译器不知道如何将您case class转换为HttpEntity.
在您的情况下,您希望将其转换为HttpEntitywith 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
}
Run Code Online (Sandbox Code Playgroud)
请注意,您可以在其他位置定义这些含义,例如a package,然后只需在测试类中导入它.这样会更好,因为你肯定想重用Marshaller.
| 归档时间: |
|
| 查看次数: |
6500 次 |
| 最近记录: |