Quantcast

Controller interceptors and unit tests

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

Controller interceptors and unit tests

Ricardo J. Méndez
Hello,

I'm trying to figure out a behavior on some controller integration
tests I have.  Imagine a Specie controller, which descends from an
AdminOnlyController:

abstract class AdminOnlyController {
        def index = { }
       
        def beforeInterceptor = [   action:     this.&auth,
                                    except: [  'list', 'show']
                                                ]
        private auth() {
            if (!session.siteUser || !session.siteUser instanceof AdminUser) {
                redirect(controller:'siteUser', action:'login')
            }
        }
       
}


class SpecieController extends AdminOnlyController {
    [.....]
}

If I run the application  and access the /specie/edit function without
an user logged on, I get correctly redirected to the /siteUser/login
action.  However, if I invoke it from a test case, it would seem that
the interceptor is ignored.  Running the following test case:

class SpecieControllerTests extends GroovyTestCase {

        // setUp goes here.....

        void testEdit() {
            def controller = new SpecieController()

            assert controller.beforeInterceptor != null
            assert controller.session.siteUser == null
            // controller.params.id=1
            controller.edit()
            assertEquals "/siteUser/login", controller.response.redirectedUrl
        }
}

Yields this error:

expected:<...iteUser/login> but was:<...pecie/list>

which would mean that the response is being redirected to specie/list,
since a specie can't be found, which means that the edit function got
executed without the interceptor being invoked.

Is this an expected behavior of intercepted closures if invoked
directly?  If so,  how should controllers like the ones above be
tested?  Any recommended reading is welcome.

Thanks in advance,


Ricardo J. Méndez
http://ricardo.strangevistas.net/

---------------------------------------------------------------------
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

Re: Controller interceptors and unit tests

Ricardo J. Méndez
I've noticed an error with the AdminController, which wasn't doing
"return false" after the redirect so it caused the intercepted action
to be executed anyway.    This solves a behavior I'd noticed when
running the application, but doens't change the behavior when testing
- controller.response.redirectedUrl is still not the correct page.

Any pointers?



On 6/8/07, Ricardo J. Méndez <[hidden email]> wrote:

> Hello,
>
> I'm trying to figure out a behavior on some controller integration
> tests I have.  Imagine a Specie controller, which descends from an
> AdminOnlyController:
>
> abstract class AdminOnlyController {
>         def index = { }
>
>         def beforeInterceptor = [   action:     this.&auth,
>                                     except: [  'list', 'show']
>                                                 ]
>         private auth() {
>             if (!session.siteUser || !session.siteUser instanceof AdminUser) {
>                 redirect(controller:'siteUser', action:'login')
>             }
>         }
>
> }
>
>
> class SpecieController extends AdminOnlyController {
>     [.....]
> }
>
> If I run the application  and access the /specie/edit function without
> an user logged on, I get correctly redirected to the /siteUser/login
> action.  However, if I invoke it from a test case, it would seem that
> the interceptor is ignored.  Running the following test case:
>
> class SpecieControllerTests extends GroovyTestCase {
>
>         // setUp goes here.....
>
>         void testEdit() {
>             def controller = new SpecieController()
>
>             assert controller.beforeInterceptor != null
>             assert controller.session.siteUser == null
>             // controller.params.id=1
>             controller.edit()
>             assertEquals "/siteUser/login", controller.response.redirectedUrl
>         }
> }
>
> Yields this error:
>
> expected:<...iteUser/login> but was:<...pecie/list>
>
> which would mean that the response is being redirected to specie/list,
> since a specie can't be found, which means that the edit function got
> executed without the interceptor being invoked.
>
> Is this an expected behavior of intercepted closures if invoked
> directly?  If so,  how should controllers like the ones above be
> tested?  Any recommended reading is welcome.
>
> Thanks in advance,
>
>
> Ricardo J. Méndez
> http://ricardo.strangevistas.net/
>


--


Ricardo J. Méndez
http://ricardo.strangevistas.net/

---------------------------------------------------------------------
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

Re: Re: Controller interceptors and unit tests

graemer
Interceptors won't automatically be invoked in your unit tests when
calling the actions

Cheers

On 6/9/07, Ricardo J. Méndez <[hidden email]> wrote:

> I've noticed an error with the AdminController, which wasn't doing
> "return false" after the redirect so it caused the intercepted action
> to be executed anyway.    This solves a behavior I'd noticed when
> running the application, but doens't change the behavior when testing
> - controller.response.redirectedUrl is still not the correct page.
>
> Any pointers?
>
>
>
> On 6/8/07, Ricardo J. Méndez <[hidden email]> wrote:
> > Hello,
> >
> > I'm trying to figure out a behavior on some controller integration
> > tests I have.  Imagine a Specie controller, which descends from an
> > AdminOnlyController:
> >
> > abstract class AdminOnlyController {
> >         def index = { }
> >
> >         def beforeInterceptor = [   action:     this.&auth,
> >                                     except: [  'list', 'show']
> >                                                 ]
> >         private auth() {
> >             if (!session.siteUser || !session.siteUser instanceof AdminUser) {
> >                 redirect(controller:'siteUser', action:'login')
> >             }
> >         }
> >
> > }
> >
> >
> > class SpecieController extends AdminOnlyController {
> >     [.....]
> > }
> >
> > If I run the application  and access the /specie/edit function without
> > an user logged on, I get correctly redirected to the /siteUser/login
> > action.  However, if I invoke it from a test case, it would seem that
> > the interceptor is ignored.  Running the following test case:
> >
> > class SpecieControllerTests extends GroovyTestCase {
> >
> >         // setUp goes here.....
> >
> >         void testEdit() {
> >             def controller = new SpecieController()
> >
> >             assert controller.beforeInterceptor != null
> >             assert controller.session.siteUser == null
> >             // controller.params.id=1
> >             controller.edit()
> >             assertEquals "/siteUser/login", controller.response.redirectedUrl
> >         }
> > }
> >
> > Yields this error:
> >
> > expected:<...iteUser/login> but was:<...pecie/list>
> >
> > which would mean that the response is being redirected to specie/list,
> > since a specie can't be found, which means that the edit function got
> > executed without the interceptor being invoked.
> >
> > Is this an expected behavior of intercepted closures if invoked
> > directly?  If so,  how should controllers like the ones above be
> > tested?  Any recommended reading is welcome.
> >
> > Thanks in advance,
> >
> >
> > Ricardo J. Méndez
> > http://ricardo.strangevistas.net/
> >
>
>
> --
>
>
> Ricardo J. Méndez
> http://ricardo.strangevistas.net/
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
http://grails.org

---------------------------------------------------------------------
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

Re: Re: Controller interceptors and unit tests

Ricardo J. Méndez
Thanks for the clarification, Graeme.   Is this something that is
expected to be added in the future?  How should controllers which
depend on interceptors be tested? (Via webtest, I imagine)


Best regards,


On 6/11/07, Graeme Rocher <[hidden email]> wrote:

> Interceptors won't automatically be invoked in your unit tests when
> calling the actions
>
> Cheers
>
> On 6/9/07, Ricardo J. Méndez <[hidden email]> wrote:
> > I've noticed an error with the AdminController, which wasn't doing
> > "return false" after the redirect so it caused the intercepted action
> > to be executed anyway.    This solves a behavior I'd noticed when
> > running the application, but doens't change the behavior when testing
> > - controller.response.redirectedUrl is still not the correct page.
> >
> > Any pointers?
> >
> >
> >
> > On 6/8/07, Ricardo J. Méndez <[hidden email]> wrote:
> > > Hello,
> > >
> > > I'm trying to figure out a behavior on some controller integration
> > > tests I have.  Imagine a Specie controller, which descends from an
> > > AdminOnlyController:
> > >
> > > abstract class AdminOnlyController {
> > >         def index = { }
> > >
> > >         def beforeInterceptor = [   action:     this.&auth,
> > >                                     except: [  'list', 'show']
> > >                                                 ]
> > >         private auth() {
> > >             if (!session.siteUser || !session.siteUser instanceof AdminUser) {
> > >                 redirect(controller:'siteUser', action:'login')
> > >             }
> > >         }
> > >
> > > }
> > >
> > >
> > > class SpecieController extends AdminOnlyController {
> > >     [.....]
> > > }
> > >
> > > If I run the application  and access the /specie/edit function without
> > > an user logged on, I get correctly redirected to the /siteUser/login
> > > action.  However, if I invoke it from a test case, it would seem that
> > > the interceptor is ignored.  Running the following test case:
> > >
> > > class SpecieControllerTests extends GroovyTestCase {
> > >
> > >         // setUp goes here.....
> > >
> > >         void testEdit() {
> > >             def controller = new SpecieController()
> > >
> > >             assert controller.beforeInterceptor != null
> > >             assert controller.session.siteUser == null
> > >             // controller.params.id=1
> > >             controller.edit()
> > >             assertEquals "/siteUser/login", controller.response.redirectedUrl
> > >         }
> > > }
> > >
> > > Yields this error:
> > >
> > > expected:<...iteUser/login> but was:<...pecie/list>
> > >
> > > which would mean that the response is being redirected to specie/list,
> > > since a specie can't be found, which means that the edit function got
> > > executed without the interceptor being invoked.
> > >
> > > Is this an expected behavior of intercepted closures if invoked
> > > directly?  If so,  how should controllers like the ones above be
> > > tested?  Any recommended reading is welcome.
> > >
> > > Thanks in advance,
> > >
> > >
> > > Ricardo J. Méndez
> > > http://ricardo.strangevistas.net/
> > >
> >
> >
> > --
> >
> >
> > Ricardo J. Méndez
> > http://ricardo.strangevistas.net/
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
>
>
> --
> Graeme Rocher
> Grails Project Lead
> http://grails.org
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--


Ricardo J. Méndez
http://ricardo.strangevistas.net/

---------------------------------------------------------------------
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

Re: Re: Controller interceptors and unit tests

graemer
Well you would test the interceptor in isolation and via webtest if necessary

Cheers

On 6/11/07, Ricardo J. Méndez <[hidden email]> wrote:

> Thanks for the clarification, Graeme.   Is this something that is
> expected to be added in the future?  How should controllers which
> depend on interceptors be tested? (Via webtest, I imagine)
>
>
> Best regards,
>
>
> On 6/11/07, Graeme Rocher <[hidden email]> wrote:
> > Interceptors won't automatically be invoked in your unit tests when
> > calling the actions
> >
> > Cheers
> >
> > On 6/9/07, Ricardo J. Méndez <[hidden email]> wrote:
> > > I've noticed an error with the AdminController, which wasn't doing
> > > "return false" after the redirect so it caused the intercepted action
> > > to be executed anyway.    This solves a behavior I'd noticed when
> > > running the application, but doens't change the behavior when testing
> > > - controller.response.redirectedUrl is still not the correct page.
> > >
> > > Any pointers?
> > >
> > >
> > >
> > > On 6/8/07, Ricardo J. Méndez <[hidden email]> wrote:
> > > > Hello,
> > > >
> > > > I'm trying to figure out a behavior on some controller integration
> > > > tests I have.  Imagine a Specie controller, which descends from an
> > > > AdminOnlyController:
> > > >
> > > > abstract class AdminOnlyController {
> > > >         def index = { }
> > > >
> > > >         def beforeInterceptor = [   action:     this.&auth,
> > > >                                     except: [  'list', 'show']
> > > >                                                 ]
> > > >         private auth() {
> > > >             if (!session.siteUser || !session.siteUser instanceof AdminUser) {
> > > >                 redirect(controller:'siteUser', action:'login')
> > > >             }
> > > >         }
> > > >
> > > > }
> > > >
> > > >
> > > > class SpecieController extends AdminOnlyController {
> > > >     [.....]
> > > > }
> > > >
> > > > If I run the application  and access the /specie/edit function without
> > > > an user logged on, I get correctly redirected to the /siteUser/login
> > > > action.  However, if I invoke it from a test case, it would seem that
> > > > the interceptor is ignored.  Running the following test case:
> > > >
> > > > class SpecieControllerTests extends GroovyTestCase {
> > > >
> > > >         // setUp goes here.....
> > > >
> > > >         void testEdit() {
> > > >             def controller = new SpecieController()
> > > >
> > > >             assert controller.beforeInterceptor != null
> > > >             assert controller.session.siteUser == null
> > > >             // controller.params.id=1
> > > >             controller.edit()
> > > >             assertEquals "/siteUser/login", controller.response.redirectedUrl
> > > >         }
> > > > }
> > > >
> > > > Yields this error:
> > > >
> > > > expected:<...iteUser/login> but was:<...pecie/list>
> > > >
> > > > which would mean that the response is being redirected to specie/list,
> > > > since a specie can't be found, which means that the edit function got
> > > > executed without the interceptor being invoked.
> > > >
> > > > Is this an expected behavior of intercepted closures if invoked
> > > > directly?  If so,  how should controllers like the ones above be
> > > > tested?  Any recommended reading is welcome.
> > > >
> > > > Thanks in advance,
> > > >
> > > >
> > > > Ricardo J. Méndez
> > > > http://ricardo.strangevistas.net/
> > > >
> > >
> > >
> > > --
> > >
> > >
> > > Ricardo J. Méndez
> > > http://ricardo.strangevistas.net/
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe from this list please visit:
> > >
> > >     http://xircles.codehaus.org/manage_email
> > >
> > >
> >
> >
> > --
> > Graeme Rocher
> > Grails Project Lead
> > http://grails.org
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
>
>
> --
>
>
> Ricardo J. Méndez
> http://ricardo.strangevistas.net/
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
http://grails.org

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

    http://xircles.codehaus.org/manage_email

Loading...