megaurav2002 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I know this question should not be here but actually i posted it on Catalyst mailing list and didn't hear anything back. So i thought some monks here will be kind enough to show me some light.

I have just started working on a project using Perl web development framework Catalyst. I am trying my hand at testing and got the following issue :(.

I have created the following test file under t/ directory:
my $c = MyApp -> prepare(); MyApp::Controller::MyController::MyAction(undef, $c); ok($c->stash->{template} eq $expected_template_name, "comparing templa +te returned");
Now, all seemed to work fine till i realized that its better to explicitly forward $c to MyApp::View::TT rather than doing in the end method. But after adding this line to my controller action i got this error:
"Modification of non-creatable array value attempted, subscript -1 at /usr/lib/perl5/vendor_perl/5.8.8/Catalyst/Dispatcher.pm line 186."
I guess this is happening because $c->stash->template does not exist anymore?

Any way to get around this problem.

Thanks a lot,
Gaurav Talwar

"Wisdom begins in wonder" - Socrates, philosopher

Replies are listed 'Best First'.
Re: Testing Catalyst Views
by Your Mother (Archbishop) on Apr 18, 2008 at 02:17 UTC

    That's a pretty peculiar way to test. I'd expect it to fail on all points. Your prepare isn't preparing anything. I barely know the internals but there is no request or CGI environment vars if you're calling it that way. I don't even know if it would have an engine chosen/present. You want Test::WWW::Mechanize::Catalyst and Catalyst::Test. Take a look at the main Runtime distribution's test directory for all kinds of nice examples.

Re: Testing Catalyst Views
by holli (Abbot) on Apr 18, 2008 at 08:17 UTC
    The standard method to test a catalyst component (here a view) is to write a test a test application for it. Given that such an app can run as a standalone server, that's not too hard.

    You might find CatalystX::Starter useful, which bootstraps an empty component, including a readymade test app.


    holli, /regexed monk/

      Nice, I didn't know about CatalystX::Starter and just did one of my own by hand the same way. Where were you when I needed you?!

      You don't always need a test application though. Maybe usually don't. They're for testing generic, typically application-free, parts like plugins and base controllers and such. If you're writing a real app, you can test it directly, don't need to mimic it in a stripped down test version.