如何在Play Framework(2.0版,使用Scala)中测试期望上传文件的操作?

Chr*_* W. 6 scala functional-testing playframework playframework-2.0

文档的编写功能测试部分非常吝啬,缺乏完全提交模拟表单值的详细信息.我以某种方式(不记得如何/在哪里)确定你可以提交基本的表单值(模拟POST请求)通过传递MapFakeRequest喜欢这样:

val Some(result) = routeAndCall(FakeRequest(POST, "/path/to/test", FakeHeaders(),
                                Map("postedVariable" -> Seq("and a value"))))
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不允许"上传"文件的情况.

Ale*_*rju 11

我们的文件上传测试看起来像这样:

val tempFile = TemporaryFile(new java.io.File("/tmp/the.file"))
val part = FilePart[TemporaryFile](key = "image", filename = "the.file", contentType = Some("image/jpeg"), ref = tempFile)
val formData = MultipartFormData(dataParts = Map(), files = Seq(part), badParts = Seq(), missingFileParts = Seq())
val result = routeAndCall(FakeRequest(POST, "/path/to/test", FakeHeaders(), formData))
Run Code Online (Sandbox Code Playgroud)

where "image"表示您希望在其中查找文件内容的HTML表单元素的名称.

如果您使用BodyParsers.maxLength限制上传的大小,可以替换formDataRight(formData)