[Catalyst] unit testing with MockObject

Matt S Trout dbix-class at trout.me.uk
Sun Jun 4 13:50:29 CEST 2006


Daniel McBrearty wrote:
>  > You can't dispatch to a controller you're mocking unless you mock the
>> internal dispatch stuff as well ...
> 
> (testcode)
> 
> BEGIN { use_ok('Catalyst::Test', 'Engoi'); }
> my $mock = Test::MockObject->new;
> $mock->set_true('home');
> Engoi->components->{'Engoi::Controller::Public'} = $mock;
> 
> clearSession();
> my $mech = Test::WWW::Mechanize::Catalyst->new;
> $mech->requests_redirectable( [] );
> 
> $r = $mech->get($dr.'public/home');
> $l = $r->header('location');
> ok( !$r->is_redirect, 'no redirect to natlang with no session');
> 
> (/testcode)
> 
> and this works apart form the error ...
> 
> Obviously I don't want to be having to simulate all the internals of a
> Controller to test ... so I try to use Test::MockObject::Extends
> instead (I only want to override one method, right ...?):
> 
> (newtestcode)
> BEGIN { use_ok('Catalyst::Test', 'Engoi'); }
> 
> my $mock = Test::MockObject::Extends->new(Engoi->components->{'Engoi::Controller::Public'}
> );
> $mock->set_true('home');
> (/newtestcode)
> 
> but now the MockObject stops working ...
> 
> maybe cat loads actions when the script starts, whatever
> MockObject::Extends does is just ignored I guess?
> 
> what am I doing wrong ... seems I am making hard work of something
> that shouldn't be *that* tough ... is there an easier way to do all
> this?
> 
> All I *really* need to do is stub out a method in a controller that I
> am not testing here, stop it redirecting itself and breaking my tests.

If you're not testing Controller::Public, why are you dispatching a 
request to "public/home" in the first place?



More information about the Catalyst mailing list