覆盖 dateCreated 以在 Grails 中进行测试 [英] Overriding dateCreated for testing in Grails

查看:23
本文介绍了覆盖 dateCreated 以在 Grails 中进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在不关闭自动时间戳的情况下覆盖我的域类中 dateCreated 字段的值?

Is there any way I can override the value of dateCreated field in my domain class without turning off auto timestamping?

我需要测试控制器,我必须提供具有特定创建日期的特定域对象,但 GORM 似乎覆盖了我提供的值.

I need to test controller and I have to provide specific domain objects with specific creation date but GORM seems to override values I provide.

我的课程如下所示:

class Message {

    String content
    String title
    User author

    Date dateCreated
    Date lastUpdated

    static hasMany = [comments : Comment]

    static constraints = {
        content blank: false
        author nullable: false
        title nullable: false, blank: false
    }

    static mapping = {
        tablePerHierarchy false
        tablePerSubclass true
        content type: "text"
        sort dateCreated: 'desc'
    }
}

class BlogMessage extends Message{

    static belongsTo = [blog : Blog]

    static constraints = {
        blog nullable: false
    }

}

我正在使用控制台来缩短时间.我用 Victor 的方法遇到的问题是,当我写的时候:

I'm using console to shorten things up. The problem which I encountered with Victor's approach is, when I write:

Date someValidDate = new Date() - (20*365)

BlogMessage.metaClass.setDateCreated = {
            Date d ->            
            delegate.@dateCreated = someValidDate
}

我得到以下异常:

groovy.lang.MissingFieldException: No such field: dateCreated for class: pl.net.yuri.league.blog.BlogMessage

当我尝试时

Message.metaClass.setDateCreated = {
                Date d ->            
                delegate.@dateCreated = someValidDate
}

脚本运行良好,但不幸的是 dateCreated 没有被更改.

Script goes well, but unfortunately dateCreated is not being altered.

推荐答案

我遇到了类似的问题,并且能够覆盖我的域的 dateCreated (在 Quartz Job 测试中,所以 Spec,Grails 上没有 @TestFor 注释2.1.0) 由

I was having a similar issue, and was able to overwrite dateCreated for my domain (in a Quartz Job test, so no @TestFor annotation on the Spec, Grails 2.1.0) by

  • 使用 BuildTestData 插件(不管怎样,我们经常使用它,非常棒)
  • 使用 save(flush:true) 双击域实例

供参考,我的测试:

import grails.buildtestdata.mixin.Build
import spock.lang.Specification
import groovy.time.TimeCategory

@Build([MyDomain])
class MyJobSpec extends Specification {

    MyJob job

    def setup() {
        job = new MyJob()
    }

    void "test execute fires my service"() {
        given: 'mock service'
            MyService myService = Mock()
            job.myService = myService

        and: 'the domains required to fire the job'
            Date fortyMinutesAgo
            use(TimeCategory) {
                fortyMinutesAgo = 40.minutes.ago
            }

            MyDomain myDomain = MyDomain.build(stringProperty: 'value')
            myDomain.save(flush: true) // save once, let it write dateCreated as it pleases
            myDomain.dateCreated = fortyMinutesAgo
            myDomain.save(flush: true) // on the double tap we can now persist dateCreated changes

        when: 'job is executed'
            job.execute()

        then: 'my service should be called'
            1 * myService.someMethod()
    }
}

这篇关于覆盖 dateCreated 以在 Grails 中进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆