为什么Grails中的Spock规范导致'projectname.Event实例的标识符从1更改为2;'?

时间:2011-01-22 13:49:04

标签: hibernate exception grails integration-testing spock

运行test-app integration:spock会导致以下错误:

  

实例的标识符   projectname.Event从1改变   到2;嵌套异常是    org.hibernate.HibernateException :实例的标识符   projectname.Event从1改变   到2

     

org.springframework.orm.hibernate3.HibernateSystemException :   。的实例的标识符   projectname.Event从1改变   到2;

     

嵌套异常是    org.hibernate.HibernateException :实例的标识符   projectname.Event从1改变   到2点   projectname.EventControllerSpec.save:   一个   事件(EventControllerSpec.groovy:74)

     

引起:    org.hibernate.HibernateException :实例的标识符   projectname.Event从1改变   到2

  1. 此问题源于何处?
  2. 如何解决?
  3. [可选]是否有人可以创建代码“spock”?

  4. 来自EventControllerSpec.groovy的

    def "save: an event"() {
        given: "Constraint-conform event properties"
        def eventTitle              = "Being in Beijing"
        controller.params.title     = eventTitle
        controller.params.details   = "Details"
        controller.params.location  = "Beijing"
        controller.params.startDate = "01.09.2030"
        controller.params.startTime = "20:15"
        controller.params.endDate   = "01.09.2030"
        controller.params.endTime   = "21:45"
        controller.params.publisher = getUserObject("someuser")
    
        when: "I save that event"
        def result = controller.save() // THIS IS LINE #74 AS STATED IN THE ERROR
    
        then: "The event is successfully saved and the show-view rendered"
        controller.flash.message.args.grep(eventTitle)
        redirectArgs.action             == "show"
        redirectArgs.id                 == result.eventInstance.id
    }
    
    private User getUserObject(String name) {
        def user = User.findByUsername(name)
    
        if (!user) {
            user = new User()
            user.username = name
            user.email = "${name}@example.com"
            user.pw = "barbar"
            user.pwConfirmation = "barbar"
            assert user.save()
        }
    
        user
    }
    
    来自EventController.groovy的

    def save = {
        def eventInstance = new Event()
    
        eventInstance.title     = params.title
        eventInstance.details   = params.details
        eventInstance.location  = params.location
        eventInstance.startDate = DateUtil.createDate(params.startDate, params.startTime)
        eventInstance.endDate   = DateUtil.createDate(params.endDate, params.endTime)
        eventInstance.publisher = session.user
    
        if (eventInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), eventInstance.title])}"
            redirect(action: "show", id: eventInstance.id)
        }
        else {
            eventInstance.errors.each { log.warn it }
            render(view: "add", model: [eventInstance: eventInstance])
        }
    }
    

1 个答案:

答案 0 :(得分:1)

您的Spock测试扩展了什么课程?它应该是spock.lang.Specificationgrails.plugin.spock.IntegrationSpec而非grails.plugin.spock.ControllerSpec,专为单元测试而设计。