[Catalyst] Testing sessions

Ovid publiustemp-catalyst at yahoo.com
Thu Feb 16 22:13:30 CET 2006


Hi all,

Feeling very silly right now, but I can't figure out what I'm doing
wrong with my session handling here.  I wrote a small test to
demonstrate the problem.  Below my signoff is the test code I wrote. 
The test output is this:

  t/catalyst/cache....ok 1 - CacheTestApp->can('session')
  # 119fa260de515e8e851d60147c319c37
  ok 2 - get ok
  ok 3 - CacheTestApp->can('session')
  # undef
  not ok 4 - We should be able to fetch items from the cache

  #   Failed test 'We should be able to fetch items from the cache'
  #   in t/catalyst/cache.t at line 51.
  not ok 5 - ... and the object it returns isa Faux::User

  #   Failed test '... and the object it returns isa Faux::User'
  #   in t/catalyst/cache.t at line 53.
  #     ... and the object it returns isn't defined

The "# undef" is the result of me trying to "diag $c->sessionid".  For
the second test, I'm not getting that sessionid and it looks like
that's why I'm not getting the information from the cache.

Cheers,
Ovid

#!/usr/bin/perl

use strict;
use warnings;

use Test::More 'no_plan';

{
    package Faux::User;

    sub new {
        my ( $class, $user ) = @_;
        bless { user => $user }, $class;
    }

    sub name {
        my $self = shift;
        return "Mr. $self->{user}";
    }
}

{
    package CacheTestApp;
    use Test::More;

    use Catalyst qw( 
         Session 
         Session::State::Cookie 
         Session::Store::File
    );

    sub store_in_cache : Local {
        my ( $self, $c ) = @_;

        can_ok $c, 'session';
        my $user = Faux::User->new('Bob');
        $c->session->{user} = $user;
        $c->res->body("ok");
    }

    sub retrieve_from_cache : Local {
        my ( $self, $c ) = @_;

        can_ok $c, 'session';
        ok my $user = $c->session->{user},
          'We should be able to fetch items from the cache';
        isa_ok $user, 'Faux::User', '... and the object it returns';
        is $user->name, 'Mr. Bob',
          '... and it should be the correct object';
        $c->res->body("ok");
    }

    __PACKAGE__->config( { 
        session => {
            expires => 3600,
            storage => '/tmp/catalyst'
        }
    } );
    __PACKAGE__->setup;
}

use Catalyst::Test qw/CacheTestApp/;

ok( get("/store_in_cache"), "get ok" );
ok( get("/retrieve_from_cache"), "get ok" );


-- 
If this message is a response to a question on a mailing list, please send follow up questions to the list.

Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/



More information about the Catalyst mailing list