I want to do test-driven development for a Dancer2 plugin I'm building. I'm a bit stymied on how to do unit testing for the plugin's functions. Here's a simplified example:
package Dancer2::Plugin::Menu ; use 5.010; use strict; use warnings; use Dancer2::Plugin; sub BUILD { my $plugin = shift; $plugin->_do_stuff } sub _do_stuff { my $plugin = shift; print Dumper $plugin; # does not print anything, undefined ... return $array_ref; }
Now let's say I want to unit test the do_stuff method in the above plugin:
use Test::Most tests => 1, 'die'; BEGIN { $ENV{'DANCER_ENVIRONMENT'} = 'testing'; } package TestApp; use Dancer2; use Dancer2::Plugin::Menu; use Data::Dumper qw 'Dumper'; get '/' => sub { return 'hello' }; get '/test' => sub { return 'hi' }; # do_stuff method gets called, but no object is passed to it so functi +on won't work properly deep_cmp [ '/', '/test' ], Dancer2::Plugin::Menu::do_stuff, 'returns e +xpected array';
As pointed out in the comment above, this doesn’t work because the plugin object doesn’t get passed to the method. One possible workaround is to export the do_stuff method and all the other methods I want to unit test with plugin_keywords but this seems hacky. Is there a better approach?
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks
In reply to How can you unit test a Dancer2 plugin mehod? by nysus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |