Skip to content

Instantly share code, notes, and snippets.

@fkumro
Created May 3, 2011 19:05
Show Gist options
  • Save fkumro/953982 to your computer and use it in GitHub Desktop.
Save fkumro/953982 to your computer and use it in GitHub Desktop.
catalyst_testing_tutorial
use strict;
use warnings;
use Test::More tests => 1;
use lib 't';
use Mock::Cache; #example of mocked object needed later
BEGIN { use_ok 'MyApp::Model::Books' }
BEGIN {
no warnings;
use Catalyst::Response;
use Catalyst::Request;
use Catalyst::Log;
use Config::General;
*MockContext::model = sub { my ($self, $model_name) = @_; $model_name = 'Mock::' . $model_name; return $model_name->new};
*MockContext::res = sub {return shift->{res}};
*MockContext::response = sub {return shift->{res}};
*MockContext::req = sub {return shift->{req}};
*MockContext::log = sub {return shift->{log}};
*MockContext::cache = sub {return Mock::Cache->new};
*MockContext::stash = sub {return shift->{stash} if scalar @_ == 1; my ($self, $key, $val) = @_; $self->{stash}->{$key} = $val};
*MockContext::config = sub {return shift->{config}};
}
my $context = bless {
stash=> {},
res=>Catalyst::Response->new,
req=>Catalyst::Request->new,
log=>Catalyst::Log->new,
config => new Config::General("myapp.conf")->{DefaultConfig},
} , 'MockContext';
my $model = MyApp::Model::Books->new;
$model->ACCEPT_CONTEXT($context);
package Mock::Authors;
use parent Catalyst::Model;
use warnings;
use strict;
use Test::MockObject;
sub search {
my ($self, $search_params) = @_;
my $mock = Test::MockObject->new();
# if the search for the author is Olen Steinhauer
# return a count of 10
if ($search_params->{author} eq 'Olen Steinhauer') {
$mock->mock('count', sub {0});
} elsif ($search_params->{author} eq 'Philip K Dick') {
# if the author search was for Philip k Dick return
# a count of 1 and a first column
my $mock_columns = Test::MockObject->new();
$mock_columns->mock('get_column', sub {'Do Androids Dream of Electric Sheep?'});
$mock->mock('first', sub {$mock_columns});
$mock->mock('count', sub { 1 });
}
return $mock;
}
my $results = $self->context()->model('Books')->search({ author => $author });
return $results->first->get_column('xxx');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment