2014-12-02 57 views
0

鑑於以下域類:如何爲Grails中的belongsTo關係創建單元測試的有效參數?

class TmMessage { 
    String message 

    static belongsTo = [bulkMessage : TmBulkMessage] 

    static constraints = { 
     message nullable: false 
    } 

    static mapping = { 
     message type: 'text' 
    } 
} 


class TmBulkMessage { 
    String messageId 
    String producerId 
    String activemqTimestamp  
    List messages 

    static hasMany = [messages: TmMessage] 

    static constraints = { 
     messageId nullable: false, unique: true 
     producerId nullable: false 
     activemqTimestamp nullable: false 
    } 

    static mapping = { 
     messages cascade: "all-delete-orphan" 
    } 
} 

Grails的產生,都給出了一系列的自動生成單元測試失敗,因爲驗證失敗的結果。我需要做些什麼來填充ValidParams()來使它工作?

@TestFor(TmMessageController) 
@Mock(TmMessage) 
class TmMessageControllerSpec extends Specification { 

    def populateValidParams(params) { 
     assert params != null 
     // TODO: Populate valid properties like... 
     //params["name"] = 'someValidName' 
     params["message"] = 'Some message' 
     params["bulkMessage"] = new TmBulkMessage(messageId: "msgId", producerId: "prodId", activemqTimestamp: "1234") 
    } 

    // the first test to fail is this one 
    void "Test the save action correctly persists an instance"() { 

     when:"The save action is executed with an invalid instance" 
      request.contentType = FORM_CONTENT_TYPE 
      request.method = 'POST' 
      def tmMessage = new TmMessage() 
      tmMessage.validate() 
      controller.save(tmMessage) 

     then:"The create view is rendered again with the correct model" 
      model.tmMessage!= null 
      view == 'create' 

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

      // this next line doesn't work, because the constraints aren't met and validate() fails 
      controller.save(tmMessage) 
      // that means that the redirectedUrl is not correct below and the test fails 

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

被調用的控制器內的save()方法是默認的自動生成的代碼看起來像:

@Transactional 
def save(TmMessage tmMessage) { 
    if (tmMessage == null) { 
     notFound() 
     return 
    } 

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

    tmMessage.save flush:true 

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

回答

0

試試這個:

params["message"] = 'Some message' 
params["bulkMessage.messageId"] = "msgId" 
params["bulkMessage.producerId"] = "prodId" 
params["bulkMessage.activemqTimestamp"] = "1234" 
+0

謝謝,這確實起作用。還遇到了一個我遇到的問題(在這裏描述:https://jira.grails.org/browse/GPSCAFFOLD-85),它要求request.format ='form'被放置在測試中。最後,我的一個測試需要使用TmBulkMessage,所以@Mock被更改爲@Mock([TmMessage,TmBulkMessage])。 – John 2014-12-02 11:25:47

相關問題