简体   繁体   中英

Problems with one-to-many association data binding in an integration test in Grails

I am trying to create an integration test and I am running in a problem in the binding of an association.

I am trying to test a save method in a controller.

I have the domain class Event

class Event {
...
..
.
Organizer organizer
}

In the controller save method I have

def save() {
...
..
.
def passedOrganizerId = params.organizer.id  // (1)
//Some comprobations

// if comprobations pass
def event = new Event(params) // (2)
.
}

The method seems to work fine but I want to create an integration test.

class EventControllerTests  extends GroovyTestCase {
    void testSave() {          
        def params = [:]
        // Params setup (3)
        controller.params.putAll(params)
        controller.save()
        ...
        ..
        .
    }

}

I have tried several ways to do the params setup but all fail.

If in the test method line (3) I enter: params.organizer.id = 3 it fails because organizer is null

If I enter params['organizer.id'] = 3 then it fails in line (1)

If I enter

params.organizer = [:]
params.organizer.id = 3

It does not fail but the databinding is not working. That it is to say event.organizer is null after line (2)

How to make the data binding work in the test method?

You can set up params in test as follow:

...
controller.params.organizer = Organizer.get(3)
controller.save()
...

ie use domain objects as controller parameters directly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM