[Catalyst] Now I can Count Sheep: Model Testing Problem Resolved

John Karr brainbuz at brainbuz.org
Thu Aug 18 06:37:18 GMT 2011


I've sorted out the problem on my own, primarily by not working on it for several days and then looking at it again.

The solution is pretty simple, I was trying to create an instance of the model subclass (BoPeep::DBI::Flock) to test, when I needed an instance of BoPeep.
	
use BoPeep ; use ...otherstuff...
# BEGIN { use_ok ' BoPeep::Model::DBI::Flock' }  

my $BoPeep = BoPeep->new ;
is( $BoPeep->model('DBI::Flock')->CountSheep(), 3 , 'CountSheep. There are 3 sheep in BoPeeps flock' ) ;



-----Original Message-----
From: John Karr [mailto:brainbuz at brainbuz.org] 
Sent: Friday, August 12, 2011 1:45 PM
To: The elegant MVC web framework
Subject: [Catalyst] Model Testing Problems.


I have a catalyst application that I've been working on for some time (it is pretty big now), and am finally trying to write tests for it. The application runs error free when run in its entirety. But I can't get Model Tests to run. My Model is DBI based, where each DSN has a MyApp::Model::DSN parent module and then there are MyApp::Model::DSN::Table child modules which contain the methods relevant to each table. I would like to access my model methods in a manner similar to how I do in an application:
 
 is( $BoPeep->model('DBI::Flock')->CountSheep(), 3 , 'CountSheep. There are 3 sheep in BoPeeps flock' ) ; _______________________________________________
List: Catalyst at lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

========================
# Parent Model
package BoPeep::Model::DBI;
 
use strict; use warnings;
use DBIx::Simple ;
use parent 'Catalyst::Model::DBI';
 
__PACKAGE__->config( .. stuff .. );

use Moose ; 
has db=>(The following is 
        is =>'ro',
        isa=>'DBIx::Simple',
        lazy_build=> 1, );
sub _build_db {
        my $self = shift ;
        return DBIx::Simple->connect($self->dbh); } 

=========================
# Method Model		
package BoPeep::Model::DBI::Flock;
use Moose;
use namespace::autoclean;
extends 'BoPeep::Model::DBI';

sub CountSheep {
	my $self = shift ;
	my $q = 'SELECT COUNT (*) FROM flock' ;
	my $countedsheep = $self->db->query( $q )->list ;
	return $countedsheep ; }

=========================
## Finally an attempt at a test script looks something like:

use strict; use warnings; use Carp::Always ;
use Test::More;

BEGIN { use_ok 'BoPeep::Model::DBI::Flock' }
use BoPeep::Model::DBI::Flock ; 
my $BoPeep = BoPeep::Model::DBI::Flock->new() || 
		die "cant make new object"; #also tried connect
ok( $BoPeep, 'Object evaluates as true!' );
is( $BoPeep->model('DBI::Flock')->CountSheep(), 3 , 'CountSheep. There are 3 sheep in BoPeeps flock' ) ; 

##############################

The use statement(s) are successful. If I comment script after use, it runs.
Looked at Catalyst::Test docs but it doesn't seem relevant here?

The error from trying to create the new instance is something like:
[info] BoPeep powered by Catalyst 5.80033
t/model_BoPeep_Flock.t .. 1/? Can't call method "log" on an undefined value at /.../perl5/.../Catalyst/Model/DBI.pm line 58
	Catalyst::Model::DBI::new('BoPeep::Model::DBI::Flock') called at t/model_BoPeep_Flock.t line 13



More information about the Catalyst mailing list