如何为Grails中的belongsTo关系创建单元测试的有效参数?

时间:2014-12-02 09:05:27

标签: unit-testing grails gorm spock

给出以下域类:

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 generate-all提供了一系列自动生成的单元测试,这些测试因验证失败而失败。我需要做些什么才能使popalParams()能够正常工作?

@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] }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

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