如何在Play scala中使用MultiPart / form-data形成数据?

时间:2016-06-30 06:15:16

标签: scala junit specifications

我不知道如何在play scala测试用例(Specs2)中发送多部分数据。 在我的项目中,方法接收多部分数据,代码看起来像这样

def school_register() = Action(parse.multipartFormData) { implicit request => 
}

在我使用Specs2的测试用例中,如何手动形成多部分数据并希望点击school_register方法。一旦方法命中值插入DB。我有四个参数regno = 100,name =“xxxx”,address =“xxx”,std = 5。没有multipart它的工作,但我必须使用多部分数据。 没有Multipart我就是这样使用

"com.example.schooladmin" should {
                "responds with 200 for addSchoolRegister action with all required parameters" in new WithApplication {
                       val controller = new TestController()
                        val result =  controller.school_registerr() .apply(FakeRequest().withFormUrlEncodedBody(
  “Reg No” -> “100”,“Name” -> “XXX”,“Address” -> “XXXXX”,“std” -> “5”))
                        status(result) must equalTo(OK)
                }
        }

1 个答案:

答案 0 :(得分:0)

以下是我这样做的方法,我创建了一个临时文件并将其与withMultipartFormDataBody一起发布:

val tempFile = TemporaryFile(new java.io.File("../server/idCards/5e7b7c6c-98b3-4245-a5fb-405c9cc904f4"))
val part = FilePart[TemporaryFile](key = "picture", filename = "the.file", contentType = Some("image/jpeg"), ref = tempFile)
val formData = MultipartFormData(dataParts = Map(), files = Seq(part), badParts = Seq(), missingFileParts = Seq())

val Some(result) = route(FakeRequest(userDomain.routes.UserController.createIdCard())
  .withMultipartFormDataBody(formData))

status(result) mustEqual OK