Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Documentation on how to use it in a unit test for a controller with usage of deep objects #23

Open
langerc opened this issue Sep 18, 2014 · 2 comments

Comments

@langerc
Copy link

langerc commented Sep 18, 2014

I use Objects that include others with the deep option.
included the configuration in my Domain Objects marshalling = { .. } configuration.

moreover i added the following to my controller setup() (from the unit test case in the project)

    grailsApplication.registerArtefactHandler(new JsonMarshallerArtefactHandler())
    grailsApplication.registerArtefactHandler(new XmlMarshallerArtefactHandler())
    defineBeans {
        convertersConfigurationInitializer(ConvertersConfigurationInitializer)
        extendedConvertersConfigurationInitializer(ExtendedConvertersConfigurationInitializer)
    }

and initialize the test before i call the action in the controller
private def initialize(){
grailsApplication.mainContext.convertersConfigurationInitializer.initialize(grailsApplication)
grailsApplication.mainContext.extendedConvertersConfigurationInitializer.initialize()
}

After adding this the objects get marshalled as json in a correct way.
I use the serializer feature to serialize enums to their names instead of the enum object to have a nice json - when i try to verify the enum in a unit test it is working in the root object but not in the objects included with deep, on those objects the unconverted enum value is returned. When i use the same object in a real controller - the serializer works.

dont know if its a bug or if i left something out in the config - cant find any clues on how to unit test a controller the proper way.

@dhalupa
Copy link
Collaborator

dhalupa commented Sep 18, 2014

On 18.09.2014 17:53, langerc wrote:

I use Objects that include others with the deep option.
included the configuration in my Domain Objects marshalling = { .. }
configuration.

moreover i added the following to my controller setup() (from the unit
test case in the project)

| grailsApplication.registerArtefactHandler(new JsonMarshallerArtefactHandler())
grailsApplication.registerArtefactHandler(new XmlMarshallerArtefactHandler())
defineBeans {
convertersConfigurationInitializer(ConvertersConfigurationInitializer)
extendedConvertersConfigurationInitializer(ExtendedConvertersConfigurationInitializer)
}
|

and initialize the test before i call the action in the controller
private def initialize(){
grailsApplication.mainContext.convertersConfigurationInitializer.initialize(grailsApplication)
grailsApplication.mainContext.extendedConvertersConfigurationInitializer.initialize()
}

After adding this the objects get marshalled as json in a correct way.
I use the serializer feature to serialize enums to their names instead
of the enum object to have a nice json - when i try to verify the enum
in a unit test it is working in the root object but not in the objects
included with deep, on those objects the unconverted enum value is
returned. When i use the same object in a real controller - the
serializer works.

dont know if its a bug or if i left something out in the config - cant
find any clues on how to unit test a controller the proper way.

What do you get as an output? Did you maybe forgot to claim all required
domain classes as mocked collaborators?


Reply to this email directly or view it on GitHub
#23.

@dhalupa
Copy link
Collaborator

dhalupa commented Sep 18, 2014

On 18.09.2014 17:53, langerc wrote:

I use Objects that include others with the deep option.
included the configuration in my Domain Objects marshalling = { .. }
configuration.

moreover i added the following to my controller setup() (from the unit
test case in the project)

| grailsApplication.registerArtefactHandler(new JsonMarshallerArtefactHandler())
grailsApplication.registerArtefactHandler(new XmlMarshallerArtefactHandler())
defineBeans {
convertersConfigurationInitializer(ConvertersConfigurationInitializer)
extendedConvertersConfigurationInitializer(ExtendedConvertersConfigurationInitializer)
}
|

and initialize the test before i call the action in the controller
private def initialize(){
grailsApplication.mainContext.convertersConfigurationInitializer.initialize(grailsApplication)
grailsApplication.mainContext.extendedConvertersConfigurationInitializer.initialize()
}

After adding this the objects get marshalled as json in a correct way.
I use the serializer feature to serialize enums to their names instead
of the enum object to have a nice json - when i try to verify the enum
in a unit test it is working in the root object but not in the objects
included with deep, on those objects the unconverted enum value is
returned. When i use the same object in a real controller - the
serializer works.

dont know if its a bug or if i left something out in the config - cant
find any clues on how to unit test a controller the proper way.


Reply to this email directly or view it on GitHub
#23.

One other thing.. notice there is an invocation of initialize() private
method from each GenericDomainClassJSONMarshallerUnitSpec which actually
iterates through domain classes and builds marshalling configuration. I
admit, it could be done better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants