2015-06-05 42 views
0

我測試了控制器的保存操作。它只是用正確的參數執行動作,但是問題出現在redirectedUrl行上:它是空的。Grails 2.4.4生成控制器測試失敗

使用該應用程序,保存域實例後,我得到重定向到show動作和show視圖正確呈現。

這裏有什麼問題的線索?

控制器:

@Transactional(readOnly = true) 
class FolderController { 

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"] 
    ... 

    @Transactional 
    def save(Folder folderInstance) { 

     if (folderInstance == null) { 
      notFound() 
      return 
     } 

     if (folderInstance.ehrId) 
     { 
      def ehr = ehr.Ehr.get(folderInstance.ehrId) 
      ehr.directory = folderInstance 
      ehr.save() 
     } 

     if (folderInstance.hasErrors()) { 
      respond folderInstance.errors, view:'create' 
      return 
     } 

     folderInstance.save flush:true 

     request.withFormat { 
      form multipartForm { 
       flash.message = message(code: 'default.created.message', args: [message(code: 'folder.label', default: 'Folder'), folderInstance.id]) 
       redirect folderInstance 
      } 
      '*' { respond folderInstance, [status: CREATED] } 
     } 
    } 
    ... 
} 

測試:

@TestFor(FolderController) 
@Mock(Folder) 
class FolderControllerSpec extends Specification { 

     ... 
    void "Test the save action correctly persists an instance"() { 

     when:"The save action is executed with a valid instance" 
      response.reset() 
      populateValidParams(params) 
      def folder = new Folder(params) 

      controller.save(folder) 
      println folder.errors // no errors 

     then:"A redirect is issued to the show action" 
      response.redirectedUrl == '/folder/show/1' 
      controller.flash.message != null 
      Folder.count() == 1 
    } 
    ... 
} 

輸出:

junit.framework.AssertionFailedError: Condition not satisfied: 

response.redirectedUrl == '/folder/show/1' 
|  |    | 
|  null   false 
org.codeh[email protected]112b2f1 

    at directory.FolderControllerSpec.Test the save action correctly persists an instance(FolderControllerSpec.groovy:61) 

回答

0

我忘了添加allowedMethods字段。

的第一個問題是,所產生的測試不將針對相應行動的權利要求的方法,所以調用.save(),這是需要的:controller.request.method =「POST」

然後@ user1690588建議了什麼(request.format ='form')訣竅以獲得正確的redirectedUrl。

我最終的測試是這樣的:

void "Test the save action correctly persists an instance"() { 

    when:"The save action is executed with a valid instance" 
     response.reset() 
     populateValidParams(params) 
     def folder = new Folder(params) 

     controller.request.method = "POST" 
     request.format = 'form' 

     controller.save(folder) 

    then:"A redirect is issued to the show action" 
     response.redirectedUrl == '/folder/show/1' 
     controller.flash.message != null 
     Folder.count() == 1 
} 
1

的Grails 支架控制器更聰明控制器。他們尊重請求格式並相應地生成響應。

例如,您的保存操作 - 如果請求格式爲form,它將重定向到顯示操作,否則返回狀態爲CREATED的已保存域實例。

下面的代碼是負責這個

request.withFormat { 
    form multipartForm { 
     flash.message = message(code: 'default.created.message', args: [message(code: 'folder.label', default: 'Folder'), folderInstance.id]) 
     redirect folderInstance 
    } 
    '*' { respond folderInstance, [status: CREATED] } 
} 

而且在你測試的情況下,你的要求是form類型不因此redirectedUrl爲空。

爲了form要求,添加以下代碼在你做保存call--

request.format = 'form' 

希望這有助於之前測試情況。

+0

正在審查的代碼,因爲它不具備支架財產,這不是一個支架控制器:https://grails.github.io/grails- doc/2.4.4/ref/Command%20Line/create-scaffold-controller.html。最初的問題是Grails沒有在測試中生成正確的請求方法,當我解決這個問題時,您對格式的建議確實有用。我希望Grails能夠在測試中生成正確的請求方法和格式,但在生成之後,仍然需要一些手動工作。 –