Unit Testing controller that accesses grailsApplication.config

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Unit Testing controller that accesses grailsApplication.config

kusako
Hi-

how would you go about unit testing a controller that uses grailsApplication.config to access config values?
I always get groovy.lang.MissingPropertyException: No such property: grailsApplication for class MyController.

Already tried tinkering with mockConfig, but haven't had much luck so far.

Thanks,

-markus
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

rlovtangen

class myTest extends  GrailsUnitTestCase {

        void testSomething() {

                mockConfig '''
            troll.station.radius_in_meters = 200
        '''
               
                <test class that uses the config>

        }


Maybe you didn't extend GrailsUnitTestCase? Thats where the mockConfig method is.

Ronny


On Dec 6, 2010, at 9:01 PM, kusako wrote:

>
> Hi-
>
> how would you go about unit testing a controller that uses
> grailsApplication.config to access config values?
> I always get groovy.lang.MissingPropertyException: No such property:
> grailsApplication for class MyController.
>
> Already tried tinkering with mockConfig, but haven't had much luck so far.
>
> Thanks,
>
> -markus
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Unit-Testing-controller-that-accesses-grailsApplication-config-tp3075213p3075213.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
>
>


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

rlovtangen
btw, since it's a controller you're testing, then it's probably best to extend ControllerUnitTestCase instead.

On Dec 6, 2010, at 9:10 PM, Ronny Løvtangen wrote:

>
> class myTest extends  GrailsUnitTestCase {
>
> void testSomething() {
>
> mockConfig '''
>             troll.station.radius_in_meters = 200
>         '''
>
> <test class that uses the config>
>
> }
>
>
> Maybe you didn't extend GrailsUnitTestCase? Thats where the mockConfig method is.
>
> Ronny
>
>
> On Dec 6, 2010, at 9:01 PM, kusako wrote:
>
>>
>> Hi-
>>
>> how would you go about unit testing a controller that uses
>> grailsApplication.config to access config values?
>> I always get groovy.lang.MissingPropertyException: No such property:
>> grailsApplication for class MyController.
>>
>> Already tried tinkering with mockConfig, but haven't had much luck so far.
>>
>> Thanks,
>>
>> -markus
>> --
>> View this message in context: http://grails.1312388.n4.nabble.com/Unit-Testing-controller-that-accesses-grailsApplication-config-tp3075213p3075213.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
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

kusako
Thanks for the quick reply.
Actually my test does extend GrailsUnitTestCase:
class MyControllerTests extends ControllerUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testIndex() {
        mockConfig '''
            my.param = 'b'
        '''
        controller.index()
    }
}

and the controller:

class MyController {

    def index = {
        render grailsApplication.config.my.param
    }
}

So I'm wondering if and how mockConfig is the correct way to mock grailsApplication for a controller.

-markus

Ronny Løvtangen wrote
btw, since it's a controller you're testing, then it's probably best to extend ControllerUnitTestCase instead.

On Dec 6, 2010, at 9:10 PM, Ronny Løvtangen wrote:

>
> class myTest extends  GrailsUnitTestCase {
>
> void testSomething() {
>
> mockConfig '''
>             troll.station.radius_in_meters = 200
>         '''
>
> <test class that uses the config>
>
> }
>
>
> Maybe you didn't extend GrailsUnitTestCase? Thats where the mockConfig method is.
>
> Ronny
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

rlovtangen
My code uses ConfigurationHolder.config.my.param instead of grailsApplication.config.my.param, but mockConfig should cover them both.
Just try to change to ConfigurationHolder.config.my.param and see if that makes any difference?

Ronny

On Dec 6, 2010, at 10:33 PM, kusako wrote:

>
> Thanks for the quick reply.
> Actually my test does extend GrailsUnitTestCase:
> class MyControllerTests extends ControllerUnitTestCase {
>    protected void setUp() {
>        super.setUp()
>    }
>
>    protected void tearDown() {
>        super.tearDown()
>    }
>
>    void testIndex() {
>        mockConfig '''
>            my.param = 'b'
>        '''
>        controller.index()
>    }
> }
>
> and the controller:
>
> class MyController {
>
>    def index = {
>        render grailsApplication.config.my.param
>    }
> }
>
> So I'm wondering if and how mockConfig is the correct way to mock
> grailsApplication for a controller.
>
> -markus
>
>
> Ronny Løvtangen wrote:
>>
>> btw, since it's a controller you're testing, then it's probably best to
>> extend ControllerUnitTestCase instead.
>>
>> On Dec 6, 2010, at 9:10 PM, Ronny Løvtangen wrote:
>>
>>>
>>> class myTest extends  GrailsUnitTestCase {
>>>
>>> void testSomething() {
>>>
>>> mockConfig '''
>>>           troll.station.radius_in_meters = 200
>>>       '''
>>>
>>> <test class that uses the config>
>>>
>>> }
>>>
>>>
>>> Maybe you didn't extend GrailsUnitTestCase? Thats where the mockConfig
>>> method is.
>>>
>>> Ronny
>>>
>>>
>>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Unit-Testing-controller-that-accesses-grailsApplication-config-tp3075213p3075370.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
>
>
>


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

bdoyle
In reply to this post by kusako
I haven't had much luck with the mockConfig myself so I went a
different route.   I just create a Map and set that on the controller
like:

def cfg = [config: [blah: "this is the value"]]

controller.grailsApplication = cfg

This works for me and it's easy peasy.

On Mon, Dec 6, 2010 at 2:33 PM, kusako <[hidden email]> wrote:

>
> Thanks for the quick reply.
> Actually my test does extend GrailsUnitTestCase:
> class MyControllerTests extends ControllerUnitTestCase {
>    protected void setUp() {
>        super.setUp()
>    }
>
>    protected void tearDown() {
>        super.tearDown()
>    }
>
>    void testIndex() {
>        mockConfig '''
>            my.param = 'b'
>        '''
>        controller.index()
>    }
> }
>
> and the controller:
>
> class MyController {
>
>    def index = {
>        render grailsApplication.config.my.param
>    }
> }
>
> So I'm wondering if and how mockConfig is the correct way to mock
> grailsApplication for a controller.
>
> -markus
>
>
> Ronny Løvtangen wrote:
>>
>> btw, since it's a controller you're testing, then it's probably best to
>> extend ControllerUnitTestCase instead.
>>
>> On Dec 6, 2010, at 9:10 PM, Ronny Løvtangen wrote:
>>
>>>
>>> class myTest extends  GrailsUnitTestCase {
>>>
>>>      void testSomething() {
>>>
>>>              mockConfig '''
>>>                      troll.station.radius_in_meters = 200
>>>              '''
>>>
>>>              <test class that uses the config>
>>>
>>>      }
>>>
>>>
>>> Maybe you didn't extend GrailsUnitTestCase? Thats where the mockConfig
>>> method is.
>>>
>>> Ronny
>>>
>>>
>>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Unit-Testing-controller-that-accesses-grailsApplication-config-tp3075213p3075370.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
>
>
>

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

Jeff Scott Brown
In reply to this post by kusako
On Mon, Dec 6, 2010 at 3:33 PM, kusako <[hidden email]> wrote:

>
> Thanks for the quick reply.
> Actually my test does extend GrailsUnitTestCase:
> class MyControllerTests extends ControllerUnitTestCase {
>    protected void setUp() {
>        super.setUp()
>    }
>
>    protected void tearDown() {
>        super.tearDown()
>    }
>
>    void testIndex() {
>        mockConfig '''
>            my.param = 'b'
>        '''
>        controller.index()
>    }
> }
>
> and the controller:
>
> class MyController {
>
>    def index = {
>        render grailsApplication.config.my.param
>    }
> }
>
> So I'm wondering if and how mockConfig is the correct way to mock
> grailsApplication for a controller.
>
> -markus
>

Markus,

The mockConfig method is defined GrailsUnitTestCase.
ControllerUnitTestCase extends MvcUnitTestCase and MvcUnitTestCase
extends GrailsUnitTestCase so the mockConfig method is available in
ControllerUnitTestCase.

I hope that helps.



jb


--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

rlovtangen
In reply to this post by bdoyle
Another option:

def mockedConfig = new ConfigObject()
mockedConfig.my.param = "b"
GrailsApplication.metaClass.getConfig = {-> mockedConfig }

ref: http://little418.com/2009/07/mocking-configuration-in-grails-for-unit-testing.html

Ronny

On Dec 6, 2010, at 10:41 PM, Brian Doyle wrote:

> I haven't had much luck with the mockConfig myself so I went a
> different route.   I just create a Map and set that on the controller
> like:
>
> def cfg = [config: [blah: "this is the value"]]
>
> controller.grailsApplication = cfg
>
> This works for me and it's easy peasy.
>
> On Mon, Dec 6, 2010 at 2:33 PM, kusako <[hidden email]> wrote:
>>
>> Thanks for the quick reply.
>> Actually my test does extend GrailsUnitTestCase:
>> class MyControllerTests extends ControllerUnitTestCase {
>>    protected void setUp() {
>>        super.setUp()
>>    }
>>
>>    protected void tearDown() {
>>        super.tearDown()
>>    }
>>
>>    void testIndex() {
>>        mockConfig '''
>>            my.param = 'b'
>>        '''
>>        controller.index()
>>    }
>> }
>>
>> and the controller:
>>
>> class MyController {
>>
>>    def index = {
>>        render grailsApplication.config.my.param
>>    }
>> }
>>
>> So I'm wondering if and how mockConfig is the correct way to mock
>> grailsApplication for a controller.
>>
>> -markus
>>
>>
>> Ronny Løvtangen wrote:
>>>
>>> btw, since it's a controller you're testing, then it's probably best to
>>> extend ControllerUnitTestCase instead.
>>>
>>> On Dec 6, 2010, at 9:10 PM, Ronny Løvtangen wrote:
>>>
>>>>
>>>> class myTest extends  GrailsUnitTestCase {
>>>>
>>>>      void testSomething() {
>>>>
>>>>              mockConfig '''
>>>>                      troll.station.radius_in_meters = 200
>>>>              '''
>>>>
>>>>              <test class that uses the config>
>>>>
>>>>      }
>>>>
>>>>
>>>> Maybe you didn't extend GrailsUnitTestCase? Thats where the mockConfig
>>>> method is.
>>>>
>>>> Ronny
>>>>
>>>>
>>>
>>
>> --
>> View this message in context: http://grails.1312388.n4.nabble.com/Unit-Testing-controller-that-accesses-grailsApplication-config-tp3075213p3075370.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
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

rlovtangen
In reply to this post by rlovtangen
The problem is that the controller does not have the grailsApplication object at all. I guess it has it for integration tests, but not for unit tests.
When I use ConfigurationHolder.config.my.param, I can run the test as a grails unit test, or as a plain junit test, and both works. But using grailsApplication.config.my.param, both fail.

Ronny

On Dec 6, 2010, at 10:40 PM, Ronny Løvtangen wrote:

> My code uses ConfigurationHolder.config.my.param instead of grailsApplication.config.my.param, but mockConfig should cover them both.
> Just try to change to ConfigurationHolder.config.my.param and see if that makes any difference?
>
> Ronny
>
> On Dec 6, 2010, at 10:33 PM, kusako wrote:
>
>>
>> Thanks for the quick reply.
>> Actually my test does extend GrailsUnitTestCase:
>> class MyControllerTests extends ControllerUnitTestCase {
>>   protected void setUp() {
>>       super.setUp()
>>   }
>>
>>   protected void tearDown() {
>>       super.tearDown()
>>   }
>>
>>   void testIndex() {
>>       mockConfig '''
>>           my.param = 'b'
>>       '''
>>       controller.index()
>>   }
>> }
>>
>> and the controller:
>>
>> class MyController {
>>
>>   def index = {
>>       render grailsApplication.config.my.param
>>   }
>> }
>>
>> So I'm wondering if and how mockConfig is the correct way to mock
>> grailsApplication for a controller.
>>
>> -markus
>>
>>
>> Ronny Løvtangen wrote:
>>>
>>> btw, since it's a controller you're testing, then it's probably best to
>>> extend ControllerUnitTestCase instead.
>>>
>>> On Dec 6, 2010, at 9:10 PM, Ronny Løvtangen wrote:
>>>
>>>>
>>>> class myTest extends  GrailsUnitTestCase {
>>>>
>>>> void testSomething() {
>>>>
>>>> mockConfig '''
>>>>           troll.station.radius_in_meters = 200
>>>>       '''
>>>>
>>>> <test class that uses the config>
>>>>
>>>> }
>>>>
>>>>
>>>> Maybe you didn't extend GrailsUnitTestCase? Thats where the mockConfig
>>>> method is.
>>>>
>>>> Ronny
>>>>
>>>>
>>>
>>
>> --
>> View this message in context: http://grails.1312388.n4.nabble.com/Unit-Testing-controller-that-accesses-grailsApplication-config-tp3075213p3075370.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
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

Burt Beckwith-2
ConfigurationHolder and the other static holders are going away in 2.0, so it'd be good to avoid using them now. It's simple to mock the grailsApplication using a hybrid approach of the above code and mockConfig:

   controller.grailsApplication = [config: config]

This way you use the config that's created by mockConfig.

Burt

On Mon, Dec 6, 2010 at 5:05 PM, Ronny Løvtangen <[hidden email]> wrote:
The problem is that the controller does not have the grailsApplication object at all. I guess it has it for integration tests, but not for unit tests.
When I use ConfigurationHolder.config.my.param, I can run the test as a grails unit test, or as a plain junit test, and both works. But using grailsApplication.config.my.param, both fail.

Ronny

On Dec 6, 2010, at 10:40 PM, Ronny Løvtangen wrote:

> My code uses ConfigurationHolder.config.my.param instead of grailsApplication.config.my.param, but mockConfig should cover them both.
> Just try to change to ConfigurationHolder.config.my.param and see if that makes any difference?
>
> Ronny
>
> On Dec 6, 2010, at 10:33 PM, kusako wrote:
>
>>
>> Thanks for the quick reply.
>> Actually my test does extend GrailsUnitTestCase:
>> class MyControllerTests extends ControllerUnitTestCase {
>>   protected void setUp() {
>>       super.setUp()
>>   }
>>
>>   protected void tearDown() {
>>       super.tearDown()
>>   }
>>
>>   void testIndex() {
>>       mockConfig '''
>>           my.param = 'b'
>>       '''
>>       controller.index()
>>   }
>> }
>>
>> and the controller:
>>
>> class MyController {
>>
>>   def index = {
>>       render grailsApplication.config.my.param
>>   }
>> }
>>
>> So I'm wondering if and how mockConfig is the correct way to mock
>> grailsApplication for a controller.
>>
>> -markus
>>
>>
>> Ronny Løvtangen wrote:
>>>
>>> btw, since it's a controller you're testing, then it's probably best to
>>> extend ControllerUnitTestCase instead.
>>>
>>> On Dec 6, 2010, at 9:10 PM, Ronny Løvtangen wrote:
>>>
>>>>
>>>> class myTest extends  GrailsUnitTestCase {
>>>>
>>>>    void testSomething() {
>>>>
>>>>            mockConfig '''
>>>>                    troll.station.radius_in_meters = 200
>>>>            '''
>>>>
>>>>            <test class that uses the config>
>>>>
>>>>    }
>>>>
>>>>
>>>> Maybe you didn't extend GrailsUnitTestCase? Thats where the mockConfig
>>>> method is.
>>>>
>>>> Ronny
>>>>
>>>>
>>>
>>
>> --
>> View this message in context: http://grails.1312388.n4.nabble.com/Unit-Testing-controller-that-accesses-grailsApplication-config-tp3075213p3075370.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
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


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

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

kusako
In reply to this post by bdoyle
Thanks, the simplest solutions usually are the best ;-)
I still had to set up the config like this:

        def cfg = [config: [my: [param: 'b']]]
        controller.metaClass.grailsApplication = cfg

As Ronny pointed out, the controller doesn't seem to have grailsApplication at all.
Looks like mockConfig is for use with services rather than controllers?

Thanks for all the help,

-markus
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

rlovtangen
In reply to this post by Burt Beckwith-2
Where does the config object come from? 

On Dec 6, 2010, at 11:11 PM, Burt Beckwith wrote:

ConfigurationHolder and the other static holders are going away in 2.0, so it'd be good to avoid using them now. It's simple to mock the grailsApplication using a hybrid approach of the above code and mockConfig:

   controller.grailsApplication = [config: config]

This way you use the config that's created by mockConfig.

Burt

On Mon, Dec 6, 2010 at 5:05 PM, Ronny Løvtangen <[hidden email]> wrote:
The problem is that the controller does not have the grailsApplication object at all. I guess it has it for integration tests, but not for unit tests.
When I use ConfigurationHolder.config.my.param, I can run the test as a grails unit test, or as a plain junit test, and both works. But using grailsApplication.config.my.param, both fail.

Ronny

On Dec 6, 2010, at 10:40 PM, Ronny Løvtangen wrote:

> My code uses ConfigurationHolder.config.my.param instead of grailsApplication.config.my.param, but mockConfig should cover them both.
> Just try to change to ConfigurationHolder.config.my.param and see if that makes any difference?
>
> Ronny
>
> On Dec 6, 2010, at 10:33 PM, kusako wrote:
>
>>
>> Thanks for the quick reply.
>> Actually my test does extend GrailsUnitTestCase:
>> class MyControllerTests extends ControllerUnitTestCase {
>>   protected void setUp() {
>>       super.setUp()
>>   }
>>
>>   protected void tearDown() {
>>       super.tearDown()
>>   }
>>
>>   void testIndex() {
>>       mockConfig '''
>>           my.param = 'b'
>>       '''
>>       controller.index()
>>   }
>> }
>>
>> and the controller:
>>
>> class MyController {
>>
>>   def index = {
>>       render grailsApplication.config.my.param
>>   }
>> }
>>
>> So I'm wondering if and how mockConfig is the correct way to mock
>> grailsApplication for a controller.
>>
>> -markus
>>
>>
>> Ronny Løvtangen wrote:
>>>
>>> btw, since it's a controller you're testing, then it's probably best to
>>> extend ControllerUnitTestCase instead.
>>>
>>> On Dec 6, 2010, at 9:10 PM, Ronny Løvtangen wrote:
>>>
>>>>
>>>> class myTest extends  GrailsUnitTestCase {
>>>>
>>>>    void testSomething() {
>>>>
>>>>            mockConfig '''
>>>>                    troll.station.radius_in_meters = 200
>>>>            '''
>>>>
>>>>            <test class that uses the config>
>>>>
>>>>    }
>>>>
>>>>
>>>> Maybe you didn't extend GrailsUnitTestCase? Thats where the mockConfig
>>>> method is.
>>>>
>>>> Ronny
>>>>
>>>>
>>>
>>
>> --
>> View this message in context: http://grails.1312388.n4.nabble.com/Unit-Testing-controller-that-accesses-grailsApplication-config-tp3075213p3075370.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
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


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

   http://xircles.codehaus.org/manage_email




Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

Burt Beckwith-2
Sorry, untested code :)  The implementation of mockConfig is

    protected void mockConfig(String config) {
        ConfigurationHolder.config = new ConfigSlurper().parse(config)
    }

so you do need to use ConfigurationHolder. But you can use them together, which is useful if the controller calls a helper that also accesses the configuration:

   mockConfig '...'
   ...
   controller.metaClass.getGrailsApplication = { -> [config: ConfigurationHolder.config]}

Burt

On Mon, Dec 6, 2010 at 5:22 PM, Ronny Løvtangen <[hidden email]> wrote:
Where does the config object come from? 


Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

rlovtangen
It would be nice if mockConfig returned the config object.
Then we wouldn't need to depend on ConfigurationHolder, but could write

def config = mockConfig('troll.station.radius_in_meters = 200')
controller.metaClass.grailsApplication = [config: config]

Should I add a Jira for that? Maybe I also make my first pull-request for grails-core...

Ronny

On Dec 6, 2010, at 11:33 PM, Burt Beckwith wrote:

Sorry, untested code :)  The implementation of mockConfig is

    protected void mockConfig(String config) {
        ConfigurationHolder.config = new ConfigSlurper().parse(config)
    }

so you do need to use ConfigurationHolder. But you can use them together, which is useful if the controller calls a helper that also accesses the configuration:

   mockConfig '...'
   ...
   controller.metaClass.getGrailsApplication = { -> [config: ConfigurationHolder.config]}

Burt

On Mon, Dec 6, 2010 at 5:22 PM, Ronny Løvtangen <[hidden email]> wrote:
Where does the config object come from? 



Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing controller that accesses grailsApplication.config

rlovtangen
Even better, if ControllerUnitTestCase had an overridden mockConfig, it could set grailsApplication with the mocked config on the controller.
Then mockConfig would work in cases where grailsApplication.config is used as well

class FooControllerTests extends ControllerUnitTestCase {

void testSomething() { mockConfig ''' my.property = "foo" ''' controller.index() }
}

class FooController { def index = { def prop = grailsApplication.config.my.property render prop } }

To add this feature:

GrailsUnitTestCase:

    protected ConfigObject mockConfig(String config) {
        ConfigurationHolder.config = new ConfigSlurper().parse(config)
        ConfigurationHolder.config
    }


ControllerUnitTestCase:

    protected ConfigObject mockConfig(String config) {
        ConfigObject conf = super.mockConfig(config)
        controller.metaClass.grailsApplication = [config: conf]
        return conf
    }

(maybe add a check if controller.metaClass.grailsApplication already is defined)


This could be done for other grails artifacts as well.

Ronny

On Dec 6, 2010, at 11:43 PM, Ronny Løvtangen wrote:

It would be nice if mockConfig returned the config object.
Then we wouldn't need to depend on ConfigurationHolder, but could write

def config = mockConfig('troll.station.radius_in_meters = 200')
controller.metaClass.grailsApplication = [config: config]

Should I add a Jira for that? Maybe I also make my first pull-request for grails-core...

Ronny

On Dec 6, 2010, at 11:33 PM, Burt Beckwith wrote:

Sorry, untested code :)  The implementation of mockConfig is

    protected void mockConfig(String config) {
        ConfigurationHolder.config = new ConfigSlurper().parse(config)
    }

so you do need to use ConfigurationHolder. But you can use them together, which is useful if the controller calls a helper that also accesses the configuration:

   mockConfig '...'
   ...
   controller.metaClass.getGrailsApplication = { -> [config: ConfigurationHolder.config]}

Burt

On Mon, Dec 6, 2010 at 5:22 PM, Ronny Løvtangen <[hidden email]> wrote:
Where does the config object come from?