[Catalyst] unit testing Chuck Norris style

Andrew Strader astrader at 2co.com
Tue Mar 27 16:01:28 GMT 2007


I have a question about writing unit tests for a Catalyst app.

I've found it's extremely useful to have internal controller tests, as
described on http://www.catalystframework.org/calendar/2006/17 ("Chuck
Norris tests his Catalyst controllers with Test::More").

The basic approach I've been using is like this:

--------------------------------
use Test::More tests => 1;
use lib 'lib';
use MyApp;
require 'mock_interface';

my $c = MyApp->new;
$c->req->param->{foo} = 3;
$c->req->param->{bar} = 4;
MyApp::Controller::MyCntrl::my_method(undef, $c);
ok( $c->stash->{baz}, 25, "calculated baz and stash'd it" );
--------------------------------

Then, what I did in mock_interface was a lot of this kind of thing:

--------------------------------
my %stash = ();
*MyApp::stash = sub { \%stash };
--------------------------------

Basically, this is a mock-up of a minimal interface for the context object,
which I pass directly to controller methods. I had to re-implement stash,
session, req->param (and all its aliases), and res->body (and all its
aliases). I may need more later.

My question is...

Is there a way to get a fully instantiated and interfaceable $c outside of
the server without having to maintaining my own separate mock implementation
of it?

I've looked at Catalyst::Test, Test::WWW::Mechanize::Catalyst, Catalyst,
Catalyst::Engine, and Catalyst::Request, but none of them seem to contain a
full-fledged $c independently of the server startup process.






More information about the Catalyst mailing list