[Catalyst] faking $c->user in tests

Ash Berlin ash_cpan at firemirror.com
Sat Jan 13 19:03:15 GMT 2007


Daniel McBrearty wrote:
> 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


Yeah that looks reasonable enough



More information about the Catalyst mailing list