Playfarmework测试:使用Fake Request和mulipartFormData上传文件

时间:2019-07-17 01:47:34

标签: scala unit-testing playframework-2.5

我正在尝试对通过multipartFormData上传的文件进行单元测试,如果文件返回了上载的URL,则返回状态200。但是,我不断获得状态400,不确定为什么它没有成功。

我发现该呼叫的在线内容必须是可写的,并且包含一个我已经作为惰性val导入的可写类

"return a 200 if successful" in new SetUp {
      val fakeFileName = "testImage.png"
      val fakeUploadImageUrl = "https://s3.fakeImageUrl.png"
      val imageFile = new File(fakeFileName)
      val tempFile = TemporaryFile(imageFile)
      val multipartFormData = new MultipartFormData(
        dataParts = Map(),
        files = Seq(FilePart[TemporaryFile]("file", fakeFileName, Some("text/plain"), tempFile)),
        badParts = Seq()
      )

      when(imageService.uploadWebImage(any(), any())).thenReturn(Future.successful(fakeUploadImageUrl))

      val request = FakeRequest(HttpVerbs.POST, s"/internal/challengesv2/v1/instances/image/$fakeFileName")
        .withBody(multipartFormData)

      val response = testController.uploadImage(fakeFileName).apply(request)

      assert(status(response.run()) === 200)
      assert(contentAsJson(response.run()) === Json.toJson(fakeUploadImageUrl))
}

期望200但返回400

0 个答案:

没有答案