[Catalyst] faking $c->user in tests

Daniel McBrearty danielmcbrearty at gmail.com
Sat Jan 13 18:38:10 GMT 2007


heh ... cheers ... think I've sussed it :

my $ue = 1;
my $username = 'NotAnAdmin';
use Test::MockObject;
my $user = Test::MockObject->new;

$user->mock( 'id',
             sub {return $username} );

package MyApp;

sub user {
  return $user;
}

sub user_exists {
  return $ue;
}

package main;
...

at least it looks sane at the mo ...


On 1/13/07, Ash Berlin <ash_cpan at firemirror.com> wrote:
> Daniel McBrearty wrote:
> > in test scripts I am able to (for instance) mess with the config my doing
> >
> > my $config = MyApp->config;
> > $config->{some_val} = 'test value';
> >
> >
> > given that I am happy that my Authentication code is well tested in
> > some other test script, is there a way to somehow bypass it, forcing
> > calls to $c->user_exists and $c->user in the module I'm testing to
> > return values which I directly control in the test script?
> >
> > thanks
> >
> > Daniel
> >
> >
>
> Slightly hacky but something like:
>
> {
>    no warnings 'redefine'; # Not sure if this will be needed in this case
>    *MyApp::user = sub { return bless {id=>'foo'}, MyApp::UserClass };
> }
>
> Might work. Or it might blow up in your face cos you looked at it funny
> - YMMV.
>
> -ash
>
> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
>


-- 
Daniel McBrearty
email : danielmcbrearty at gmail.com
www.engoi.com : the multi - language vocab trainer
BTW : 0873928131



More information about the Catalyst mailing list