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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.