Controller Unit Test problem while params as a map

classic Classic list List threaded Threaded
8 messages Options
Jet
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Controller Unit Test problem while params as a map

Jet
Hi All,

When running controller unit test. Setting the params individually like below works fine

controller.params.firstname = 'joe'
controller.params.lastname = 'blog'


However, if setting the params using a map like below:

controller.params = [firstname: 'joe', lastname: 'blog']

It would throw exceptions:

groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: params for class:

any suggestions?

Jet
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Controller Unit Test problem while params as a map

burtbeckwith
The obvious suggestion is to set the params individually.

Burt

>
> Hi All,
>
> When running controller unit test. Setting the params individually like
> below works fine
>
> controller.params.firstname = 'joe'
> controller.params.lastname = 'blog'
>
>
> However, if setting the params using a map like below:
>
> controller.params = [firstname: 'joe', lastname: 'blog']
>
> It would throw exceptions:
>
> groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: params
> for class:
>
> any suggestions?
>
> Jet
>

signature.asc (205 bytes) Download Attachment
Jet
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Controller Unit Test problem while params as a map

Jet
Hi Burt,

Thanks for the reply.

It would be nice if the future version of grails can handle setting the params as a map in the test, otherwise setting those params can be tedious if the required property for the domain class is 10+.

However, if I do

println controller.params

It outputs a map.

Jet
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Controller Unit Test problem while params as a map

Richard Bradley
The params are a read-only property, both during unit tests and normally.

The value is a GroovyPageAttributes, not a normal map (supports the "int(key)" and "boolean(key)" methods etc)

See
http://www.google.com/codesearch/p?hl=en#lka_1PIl4qs/src/java/grails/test/MockUtils.groovy&q=mockcon&exact_package=git://github.com/grails/grails-core.git&l=300

If you really must override it to a map of your own, you can do something like:

  controller.metaClass.getParams = {-> myMap}

You could also use a "with" block if you find writing "params." tedious, or possibly try params.putAll(myMap)?


-----Original Message-----
From: Jet [mailto:[hidden email]]
Sent: 12 November 2010 17:44
To: [hidden email]
Subject: [grails-user] Re: Controller Unit Test problem while params as a map


Hi Burt,

Thanks for the reply.

It would be nice if the future version of grails can handle setting the
params as a map in the test, otherwise setting those params can be tedious
if the required property for the domain class is 10+.

However, if I do

println controller.params

It outputs a map.

Jet
--
View this message in context: http://grails.1312388.n4.nabble.com/Controller-Unit-Test-problem-while-params-as-a-map-tp3039994p3040018.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



softwire
Richard Bradley
Tel : 020 7485 7500 ext 230 | Fax : 020 7485 7575
Web : www.softwire.com | E-mail : [hidden email]
Addr : 325 Highgate Studios, 53-79 Highgate Road, London NW5 1TL

Softwire Technology Limited. Registered in England no. 3824658. Registered Office : 13 Station Road, London N3 2SB


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Controller Unit Test problem while params as a map

Amit Jain-2
Hi Jet,

To initialize multiple params in a test case, I generally do the following way which works better.

Map myParams = [firstname: 'joe', lastname: 'blog']

controller.params.putAll(myParams)

 

On Sat, Nov 13, 2010 at 12:00 AM, Richard Bradley <[hidden email]> wrote:
The params are a read-only property, both during unit tests and normally.

The value is a GroovyPageAttributes, not a normal map (supports the "int(key)" and "boolean(key)" methods etc)

See
http://www.google.com/codesearch/p?hl=en#lka_1PIl4qs/src/java/grails/test/MockUtils.groovy&q=mockcon&exact_package=git://github.com/grails/grails-core.git&l=300

If you really must override it to a map of your own, you can do something like:

 controller.metaClass.getParams = {-> myMap}

You could also use a "with" block if you find writing "params." tedious, or possibly try params.putAll(myMap)?


-----Original Message-----
From: Jet [mailto:[hidden email]]
Sent: 12 November 2010 17:44
To: [hidden email]
Subject: [grails-user] Re: Controller Unit Test problem while params as a map


Hi Burt,

Thanks for the reply.

It would be nice if the future version of grails can handle setting the
params as a map in the test, otherwise setting those params can be tedious
if the required property for the domain class is 10+.

However, if I do

println controller.params

It outputs a map.

Jet
--
View this message in context: http://grails.1312388.n4.nabble.com/Controller-Unit-Test-problem-while-params-as-a-map-tp3039994p3040018.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



softwire
Richard Bradley
Tel : 020 7485 7500 ext 230 | Fax : 020 7485 7575
Web : www.softwire.com | E-mail : [hidden email]
Addr : 325 Highgate Studios, 53-79 Highgate Road, London NW5 1TL

Softwire Technology Limited. Registered in England no. 3824658. Registered Office : 13 Station Road, London N3 2SB


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

   http://xircles.codehaus.org/manage_email





--
Best Regards,
Amit Jain
IntelliGrape Software
Jet
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Controller Unit Test problem while params as a map

Jet
Hi Amit & Richard,

Thanks a lot for the tips, it works, less typing, and time saving.

Jet
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Controller Unit Test problem while params as a map

Jean Barmash 1
In reply to this post by Jet

@@@@@@@vv@@v

On Nov 12, 2010 12:27 PM, "Jet" <[hidden email]> wrote:
>
> Hi All,
>
> When running controller unit test. Setting the params individually like
> below works fine
>
> controller.params.firstname = 'joe'
> controller.params.lastname = 'blog'
>
>
> However, if setting the params using a map like below:
>
> controller.params = [firstname: 'joe', lastname: 'blog']
>
> It would throw exceptions:
>
> groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: params
> for class:
>
> any suggestions?
>
> Jet
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Controller-Unit-Test-problem-while-params-as-a-map-tp3039994p3039994.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
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Controller Unit Test problem while params as a map

eduardoarantes
In reply to this post by Amit Jain-2
How about complex params like:

params.lead.id

which would fill a Lead attribute in a Purchase object?

Also, how do I create the Lead object the controller will find when I create the Purchase like

new Purchase(params)

Regards
Loading...