2015-11-17 85 views
0

我正在使用Grails的功能測試插件爲我的RESTful API項目準備功能測試用例。功能測試:通過Grails中的REST API上傳文件

我無法使用適用於我的案例中的其他所有方法的技術上傳文件。

class CreateFunctionalSpec{ 

final String CREATE_API = '/document-file' 

def "Upload Document to temporary location"() { 

    File nfile = new File('test/test-data/myfile.jpg') 
    nfile.createNewFile() 

    when: 
    post("$RESTFUL_API_BASE_URL${CREATE_API}") { 
     headers['Accept'] = 'application/json' 
     headers['Authorization'] = authHeader() 
     body{ 
      "file:nfile"  
     } 
    } 


    then: 
     assert file 
}} 

我不確定如何將文件放入正文中,我嘗試將它添加爲參數,但沒有任何作用。

回答

1

此代碼有效!

def "Upload Document to temporary location"() { 

    setup: 
    def testDocument = new File('test/test-data/test-document.jpg') 

    when: 
    post("$RESTFUL_API_BASE_URL${BDM_CREATE_API}") { 
     headers['Accept'] = 'application/json' 
     headers['Authorization'] = authHeader() 
     body{ 
      setProperty("file",testDocument) 
     } 
    } 
    then: 
    201 == response.status 
    def jsonData = JSON.parse response.text 
    jsonData["status"]=="Success" 

}