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

发布于 2024-10-13 16:01:17 字数 2884 浏览 3 评论 0原文

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

实例的标识符 项目名称.事件从 1 更改为 至 2;嵌套异常是 org.hibernate.HibernateException:实例的标识符 项目名称.事件从 1 更改为 至 2

org.springframework.orm.hibernate3.HibernateSystemException: 实例的标识符 项目名称.事件从 1 更改为 至 2;

嵌套异常是 org.hibernate.HibernateException:实例的标识符 项目名称.事件从 1 更改为 至 2 时 项目名称.EventControllerSpec.保存: 一个 事件(EventControllerSpec.groovy:74)

原因: org.hibernate.HibernateException:实例的标识符 项目名称.事件从 1 更改为 至 2

  1. 此问题源于何处?
  2. 如何解决?
  3. [可选] 有人可以创建标签“spock"?

来自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])
    }
}

Running test-app integration:spock causes the following error:

identifier of an instance of
projectname.Event was altered from 1
to 2; nested exception is
org.hibernate.HibernateException: identifier of an instance of
projectname.Event was altered from 1
to 2

org.springframework.orm.hibernate3.HibernateSystemException:
identifier of an instance of
projectname.Event was altered from 1
to 2;

nested exception is
org.hibernate.HibernateException: identifier of an instance of
projectname.Event was altered from 1
to 2 at
projectname.EventControllerSpec.save:
an
event(EventControllerSpec.groovy:74)

Caused by:
org.hibernate.HibernateException: identifier of an instance of
projectname.Event was altered from 1
to 2

  1. Where does this issue stem from?
  2. How can it be resolved?
  3. [OPTIONAL] May somebody create the Tag "spock"?

from 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
}

from 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])
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2024-10-20 16:01:17

您的 Spock 测试扩展了什么类别?它应该是 spock.lang.Specification
grails.plugin.spock.IntegrationSpec 而不是 grails.plugin.spock.ControllerSpec,后者专为单元测试而设计。

What class does your Spock test extend? It should be spock.lang.Specification or
grails.plugin.spock.IntegrationSpec rather than grails.plugin.spock.ControllerSpec, which is designed for unit tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文