[Catalyst] Mock dbix search/count functionality for model testing

Trevor Leffler tleffler at uw.edu
Fri Apr 29 19:14:35 GMT 2011


Hi Frank,

Have you mocked $c->model so that you get your mock, $pin_attempt_mock, 
when you call $c->model('Foo') ?

You can either create a MockObject for $c, or you can locally override 
$c's model method.  I tend to do the former.

--Trevor


Franklin Kumro Jr wrote:
> Hello,
> 
> I have a model (MyApp/Models/Bar.pm) which retrieves data from the db 
> using the generated schema files (in MyApp/Schema/Result/*.pm).
> 
> My plan is to mock the classes which are called when retrieving data 
> from the database.
> 
> BEGIN {
>       my $resultset_mock = Test::MockObject->new();
>       $resultset_mock->fake_module('DBIx::Class::ResultSet' => ( 'count' 
> => sub { 0 } ));
> 
>       my $pin_attempt_mock = Test::MockObject->new();
>       $pin_attempt_mock->fake_module('MyApp::Schema::Result::Foo' => 
> ('search' => sub { bless $resultset_mock, 'DBIx::Class::ResultSet' }));
> }
> 
> 
> BEGIN { use_ok 'MyApp::Model::Bar' }
> 
> is (MyApp::Model::Bar->_get_failed_challenges('test', 'test'),
>     0,
>     'get failed challenges'
>     );
> 
> MyApp::Model::Bar->_get_failed_challenges contains calls to search and 
> count (shown below)
> 
> my $result = $c->model('Foo')->search({...});
> $result->count();
> 
> If I call MyApp::Schema::Result::Foo->search()->count() the mock returns 
> 0 (and other values that I tested). However when I attempt to use test 
> the Bar model the following error is returned.
> 
> "You must pass a package name and it cannot be blessed at 
> /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi/Class/MOP/Class.pm 
> line 41"
> 
> Also if I follow the stack trace down into the MyApp files, it points to 
> the line in MyApp::Model::Bar->_get_failed_challenges which calls search.
> 
> Any help would be appreciated!
> 
> Thanks,
> 
> -- Frank




More information about the Catalyst mailing list