gpt4 book ai didi

scala - 类型不匹配 : found String, 需要 io.gadling.core.validation.Validation[String]

转载 作者:行者123 更新时间:2023-12-02 22:32:44 25 4
gpt4 key购买 nike

免责声明:我对 Scala 不太熟悉,所以我可能做了一些愚蠢的事情。

我们使用 Ga特林 进行性能测试。我目前正在尝试让它向我们的 API 提交 POST 请求,使用如下内容:

exec(http("post request")
.post("http://ourApi")
.body(
StringBody(
session => """{ "myContent": "value" }""" // 1
)
)
.asJSON
.check(status.is(200))
)

如您所见,我在标记为 //1 的行处使用了 StringBody 的表达式函数,这应该是可能的 according to the Gatling documentation .

但是,当我尝试运行该程序时,我在该行上收到 ZincCompiler 错误:

type mismatch;
found : String("{ \"myContent\": \"value\" }")
required: io.gatling.core.validation.Validation[String]

为什么需要Validation[String]?在文档中我也只看到字符串...

最佳答案

您链接的页面显示

Expression

Most Gatling DSL methods actually takes Expression[T] parameters, which is a type alias for Session => Validation[T].

How is it that one can also pass Strings and other values then?

The reason is that there are implicit conversions:

  • when passing a String, it gets automagically parsed turn them into Expressions thanks to Gatling EL compiler.

  • when passing a value of another type, it gets automagically wrapped into an Expression that will always return this static value.

所以问题是由于某种原因没有触发隐式转换。来自 http://gatling.io/docs/2.2.2/session/validation.html#validation ,你可以尝试:

  1. 添加导入 io.gadling.commons.validation._

  2. 如果这没有帮助,请显式使用 Success("""{ "myContent": "value"}""")

关于scala - 类型不匹配 : found String, 需要 io.gadling.core.validation.Validation[String],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37937358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com