4

我有一个控制器,它将文件作为参数的一部分。我想知道我该如何测试这个?

我的控制器动作:

def save () {
  def colorInstance = new Color(params.color)
  CommonsMultipartFile file = request.getFile('color.filename')
  fileUploadService.upload(colorInstance, file, "foldername")
  if (requestInstance.save(flush: true)) {
    withFormat {
      html {redirect(action: "list") }
      js {render "test"}
    }
  }
}

我从这样的事情开始:...

import org.junit.Before
import grails.test.mixin.Mock
import grails.test.mixin.TestFor

@TestFor(ColorController)
@Mock(Color)
class ColorControllerTests {

    @Before
    void setUp() {
        controller.fileUploadService = new FileUploadService
    }
}

问题

  • 我不知道如何测试CommonsMultipartFile文件上传。
  • 此外,此测试用例将位于unit测试文件夹中。我该如何执行它?
4

3 回答 3

6

我正在使用 Grails 2.4,您可以在单元测试中简单地使用GrailsMockMultipartFilerequest.addFile方法。

此代码适用于带有 Spock 测试框架的 Grails 2.4.4:

控制器端:

class FileUploadController {

    def upload() {
        def multipartFile = request.getFile('requestParamName')
        if (multipartFile.empty) {
            flash.message = 'Error: file cannot be empty'
            render(view: 'upload')
            return
        }

        // do something now with your file
    }
}

单元测试端:

import grails.test.mixin.TestFor
import org.codehaus.groovy.grails.plugins.testing.GrailsMockMultipartFile
import spock.lang.Specification

@TestFor(FileUploadController)
class FileUploadControllerSpec extends Specification {

    void "upload should add flash error message if empty file in request"() {
        given:
        def multipartFile = new GrailsMockMultipartFile('requestParamName', 'someExcelFile.xls', 'application/vnd.ms-excel', new byte[0])
        request.addFile(multipartFile)

        when:
        controller.upload()

        then:
        assertEquals('Error: file cannot be empty', flash.message)
    }

}
于 2014-12-04T16:13:43.307 回答
5

由于请求将multipart在文件上传期间进行,因此实际请求 servlet 将是MultipartHttpServletRequest. 对于单元测试用例,与inmock相同并使用它。成功模拟后,您应该能够在测试和.requestcontrolleraddFilegetFileaction

import org.junit.Before
import grails.test.mixin.Mock
import grails.test.mixin.TestFor

@TestFor(ColorController)
@Mock(Color)
class ColorControllerTests {

    @Before
    void setUp() {
        controller.fileUploadService = new FileUploadService
        //Mock MultipartHttpServletRequest somethign like below
        controller.metaClass.request = mockFor(MultipartHttpServletRequest).createMock()
    }

    void testFileUpload(){
         //Add a mock multipart file to request
         controller.request.addFile(new MockMultipartFile('myFile', 'IronMan3.jpg', 'image/jpeg', "1234567" as byte[]))

         //call the controller action
         //assert response
    }
}
于 2013-05-13T01:37:53.380 回答
1

接受的答案对我不起作用。下面的代码做了:

import org.junit.Before
import grails.test.mixin.Mock
import grails.test.mixin.TestFor
import org.springframework.mock.web.MockMultipartHttpServletRequest
import org.springframework.mock.web.MockMultipartFile

@TestFor(ColorController)
class ColorControllerTests {

    def mockRequest

    @Before
    void setUp() {
        mockRequest = new MockMultipartHttpServletRequest()
        controller.metaClass.request = mockRequest

        // your service here
        controller.fileUploadService = new FileUploadService
    }

    void testFileUpload(){
         //Add a mock multipart file to request
         mockRequest.addFile(new MockMultipartFile('myFile', 'IronMan3.jpg', 'image/jpeg', "1234567" as byte[]))

         //call the controller action
         //assert response
    }
}
于 2014-04-14T14:31:50.867 回答