I have this class ```class ImageToTextResponse pr...
# kotest
c
I have this class
Copy code
class ImageToTextResponse
private constructor(
    val errorId: ErrorId,
    val status: Status,
    val solution: ImageToTextSolution,
    val taskId: TaskId
) {
  companion object {
    operator fun invoke(obj: JsonObject): EitherNel<Throwable, ImageToTextResponse> {
      fun Raise<Throwable>.mkSolution(): ImageToTextSolution {
        val solutionJson = obj.getValue("solution").jsonObject
        val confidence = Confidence(solutionJson.getValue("confidence").toString()).bind()
        val text = CaptchaText(solutionJson.getValue("text").toString()).bind()

        return ImageToTextSolution(confidence, text)
      }

      return either {
        zipOrAccumulate(
            { ErrorId(obj.getValue("errorId").toString()).bind() },
            { Status(obj.getValue("status").toString()).bind() },
            { mkSolution() },
            { TaskId(obj.getValue("taskId").toString()).bind() }) { eid, status, solution, tid ->
              ImageToTextResponse(eid, status, solution, tid)
            }
      }
    }
  }
}
I wrote a
kotest
FunSpec
test for it.
Copy code
test("Missing errorId in Json gives Left of ImageToTextResponse") {
        Arb.bind(genMaybeErrorId, genMaybeStatus, genMaybeImageToTextSolution, genMaybeTaskId) { a, b, c, d -> Tuple4(a, b, c, d) }
          .forAll { (_, b, c, d) ->
            either {
              val status = b.bind()
              val solution = c.bind()
              val taskId = d.bind()

              buildJsonObject {
                put("status", Json.encodeToJsonElement(status.value))
                put("solution", Json.encodeToJsonElement(solution))
                put("taskId", Json.encodeToJsonElement(taskId.value))
              }
            }
              .mapLeft { nonEmptyListOf(Throwable()) }
              .flatMap { ImageToTextResponse(it) }
              .isLeft() shouldBe true
          }
      }
When I run the test, I get the error
Cannot create an instance of the class ImageToTextResponse. Specs must have a public zero-arg constructor.
I've created tests for `value class`es in this way. What is different this time? Does
ImageToTextResponse
being a class completely explain this? Why?
c
There’s no value class in that code and the constructor for the spec isn’t shown (as noted in the error message).
c
@Chris Lee, why is it being a class vs value class important? Can you point me to documentation to illuminate the exact problem and the best fix?
c
You’d indicated it was a value class. Hard to advise on possible issues without correct context.
c
My other tests were with value classes.
ImageToTextResponse
is just a class. That is the only notable difference between my failing tests and the working ones that I can see, @Chris Lee.
Regarding this error, the problem was IntelliJ's Debug/Run Configuration for the test. The error was not with
kotest
or the code.