[Catalyst] unit testing Chuck Norris style

Matt Lawrence matt.lawrence at ymogen.net
Tue Apr 17 17:37:31 GMT 2007


Matt Lawrence wrote:
> Andrew Strader wrote:
>   
>> 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:
>>   
>>     
> [snip]
>   
>> 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.
>>   
>>     
> Just noticed this unanswered question in Phaylon's very helpful summary..
>
> When I wanted to do something similar, I did:
>
>
> my $controller = MyApp->controller('MyController');
> my $c = MyApp->prepare();
>
> # Monkey with $c to set up a fake context (set req->uri, or params)
>
> my $result = $controller->method_to_test($c, @args);
>
>
> Which worked for me in the very narrow context I was working in (I just
> wanted to check the return value of the method).
>
> I'm sure someone more familiar with the internals of Catalyst could tell
> you more about the pros and cons of this approach. One obvious drawback
> is the inability to forward to other actions from the one you're testing.
>
>   
Scratch that. I've just realised that you can forward in the tested
action if you call it with $c->forward in the test code, instead of
calling the method directly.

Matt



More information about the Catalyst mailing list