Overriding dateCreated for testing in Grails

后端 未结 10 1402
日久生厌
日久生厌 2021-02-12 12:27

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

I need to test controller and I ha

10条回答
  •  误落风尘
    2021-02-12 13:15

    I couldn't get the above techniques to work, the call to GrailsDomainBinder.getMapping always returned null???

    However...

    You can use the fixtures plugin to set the dateCreated property on a domain instance

    The initial loading will not do it...

    fixture {
        // saves to db, but date is set as current date :(
        tryDate( SomeDomain, dateCreated: Date.parse( 'yyyy-MM-dd', '2011-12-25') )
    }
    

    but if you follow up with a post handler

    post {
        // updates the date in the database :D
        tryDate.dateCreated = Date.parse( 'yyyy-MM-dd', '2011-12-01')
    }
    

    Relevant part of the fixtures docs here

    AFAIK fixtures don't work for unit testing, although the plugin authors may add unit testing support in the future.

提交回复
热议问题