类型不匹配:找到字符串,需要 io.gatling.core.validation.Validation[String]

Type mismatch: found String, required io.gatling.core.validation.Validation[String]

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

我们使用 Gatling 进行性能测试。我目前正在尝试让它向我们的 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. 添加import io.gatling.commons.validation._.

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