vendredi 24 avril 2015

How can I unit test a controller using altered default date binding?

How can I simulate the behavior of grails.databinding.dateFormats in a controller spock test?

In my Config.groovy, I added these settings:

grails.databinding.dateFormats = [
    'yyyy-MM-dd\'T\'HH:mm:ss.SSSX',
    'yyyy-MM-dd\'T\'HH:mm:ssX',
]

What do I have to do to get the unit test framework to bind date properties using one of the specified formats?

My controller looks like:

class MyController {
    static responseFormats = ['json']

    def get() {
        MyCommand command = bindData(new MyCommand(), params)

        command.validate()
        if (command.hasErrors()) {
            ...
        }
        else {
            //...do stuff with command object
        }
    }

    @Validateable
    class MyCommand {
        Long resourceId
        Date startDate
        Date endDate
        static constraints = {
            resourceId nullable: false
            startDate nullable: false
            endDate nullable: false
        }
    }
}

When I run the application, this code works normally; however, when bindData() is called in the unit test, the 'resourceId' property on the command object is populated, while the date properties are not.

Aucun commentaire:

Enregistrer un commentaire