[Catalyst] Testing View

Jonathan Rockway jon at jrock.us
Sun Apr 20 06:09:27 BST 2008


* On Sat, Apr 19 2008, Yao Wang wrote:
> Hi,
>
> Thanks Matt. Now i know i have used wrong way to call a method in the controller.
>
> However ,the reason i use $c->prepare is for unit testing. I have seen
> Catalyst::Test and Test::WWW::Mechanize::Catalyst, i think they are
> the testing tools for application, not for unit testing. When i do the
> unit testing, i need to pass $c and some other params to the methods.
> So can anybody tell me how to create this kind of $c for testing only
> ?

If you want to do what you described in your previous message, you need
to take a different approach.

I recommend something like this:

  use Catalyst::Test 'MyApp';

  my $template;
  my $old_process = \&YourApp::View::Whatever::process;
  *YourApp::View::Whatever::process = sub {
     my ($self, $c, @args) = @_;
     $template = $c->stash->{template};
     $self->$old_process($c, @args);
  }
  
  request('foo');
  is $template, 'the_template_for_foo.tt';

You really want to keep this kind of testing to a minimum.  The Catalyst
part of your application should be simple enough to not require
intensive testing.

Test::WWW::Mechanize::Catalyst (and friends) is mostly good for a quick
sanity check.  The important parts of your application should reside in
easily-testable classes, and Catalyst should just be a few lines of code
to make the those classes available to web users.

Regards,
Jonathan Rockway

-- 
print just => another => perl => hacker => if $,=$"



More information about the Catalyst mailing list