Grails 2.3: Child object not saved in unit test

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Grails 2.3: Child object not saved in unit test

mraible
Hello,

I'm trying to upgrade my app from Grails 2.2.1 to Grails 2.3.0. I have a controller method that looks as follows:

    def save() {
        def directionInstance = new Direction(params)
        if (!directionInstance.save(flush: true)) {
            render(view: "create", model: [directionInstance: directionInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'direction.label', default: 'Direction'), directionInstance.instruction])
        redirect(controller: "route", action: "edit", id: directionInstance.route.id)
    }

My unit test for this method:

    void testSave() {
        controller.save()

        assert model.directionInstance != null
        assert view == '/direction/create'

        response.reset()

        populateValidParams(params)
        controller.save()

        assert response.redirectedUrl == '/route/edit/12'
        assert controller.flash.message != null
        assert Direction.count() == 1
    }

    static def populateValidParams(params) {
        assert params != null
        params["stepNumber"] = '1'
        params["instruction"] = 'Go right at the river'
        params["route.name"] = "Chainsaw Loop"
        params["route.id"] = "12"
    }

This worked in 2.2.1. In 2.3, I get the following error:

assert response.redirectedUrl == '/route/edit/12'
       |        |             |
       |        /route/edit   false
       org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletResponse@4f345076

If I print directionInstance.route(where 12 should be coming from), I get:

happytrails.Route : (unsaved)

Shouldn't the save be cascaded to child objects? Or is there something new I need to do in 2.3 to make this happen?

Don't know if this matters, but my test has the following annotations at the top:

@TestFor(DirectionController)
@Mock([Direction, Route])

Thanks,

Matt
Reply | Threaded
Open this post in threaded view
|

Re: Grails 2.3: Child object not saved in unit test

Graeme Rocher-2
This would happen too if you upgraded to 2.2.4

For security reasons identifiers are not bindable in recent versions of Grails. You can make them bindable as follows:


However, it isn't recommended as you open up a potential security hole in your app

Cheers


On Fri, Sep 20, 2013 at 6:17 PM, mraible <[hidden email]> wrote:
Hello,

I'm trying to upgrade my app from Grails 2.2.1 to Grails 2.3.0. I have a
controller method that looks as follows:

    def save() {
        def directionInstance = new Direction(params)
        if (!directionInstance.save(flush: true)) {
            render(view: "create", model: [directionInstance:
directionInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args:
[message(code: 'direction.label', default: 'Direction'),
directionInstance.instruction])
        redirect(controller: "route", action: "edit", id:
directionInstance.route.id)
    }

My unit test for this method:

    void testSave() {
        controller.save()

        assert model.directionInstance != null
        assert view == '/direction/create'

        response.reset()

        populateValidParams(params)
        controller.save()

        assert response.redirectedUrl == '/route/edit/12'
        assert controller.flash.message != null
        assert Direction.count() == 1
    }

    static def populateValidParams(params) {
        assert params != null
        params["stepNumber"] = '1'
        params["instruction"] = 'Go right at the river'
        params["route.name"] = "Chainsaw Loop"
        params["route.id"] = "12"
    }

This worked in 2.2.1. In 2.3, I get the following error:

assert response.redirectedUrl == '/route/edit/12'
       |        |             |
       |        /route/edit   false

org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletResponse@4f345076

If I print directionInstance.route(where 12 should be coming from), I get:

happytrails.Route : (unsaved)

Shouldn't the save be cascaded to child objects? Or is there something new I
need to do in 2.3 to make this happen?

Don't know if this matters, but my test has the following annotations at the
top:

@TestFor(DirectionController)
@Mock([Direction, Route])

Thanks,

Matt




--
View this message in context: http://grails.1312388.n4.nabble.com/Grails-2-3-Child-object-not-saved-in-unit-test-tp4649385.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Graeme Rocher
Grails Project Lead
SpringSource