[dancer-users] How do you test the internal methods of a plugin?
Steve Dondley
sdondley at gmail.com
Sat Nov 10 14:16:45 GMT 2018
OK, got it now. Had to take things down to a lower level to create an app
object:
my $app = Dancer2::Core::App->new();
$app->with_plugins( 'Menu' );
$app->add_route(
method => 'get',
regexp => '/test',
code => sub {return 'hi'},
);
Dancer2::Plugin::Menu::_do_stuff($app);
Maybe this is useful for an advent calendar entry?
On Sat, Nov 10, 2018 at 8:08 AM Steve Dondley <sdondley at gmail.com> wrote:
> Hmm, nope. That doesn't put the routes in the object.
>
> On Sat, Nov 10, 2018 at 8:00 AM Steve Dondley <sdondley at gmail.com> wrote:
>
>> OK, I think I found a decent solution. I turned the app into an object
>> that I can pass to the the full qualified name of plugin's method.
>>
>> package TestApp; use Dancer2; use Dancer2::Plugin::Menu; use Data::Dumper
>> qw 'Dumper'; get '/' => sub { return 'hello' }; get '/test' => sub { return
>> 'hi' }; sub new { my $class = __PACKAGE__; return bless {}, $class; } my
>> $app = new(); Dancer2::Plugin::Menu::_do_stuff($app);
>>
>> On Fri, Nov 9, 2018 at 10:29 AM Steve Dondley <sdondley at gmail.com> wrote:
>>
>>> I'm trying to test the internal methods of a plugin in the context of a
>>> running test app. The following doesn't work but should give you an idea of
>>> what I want to accomplish:
>>>
>>> use Plack::Test;
>>> use HTTP::Request::Common;
>>> { package TestApp;
>>> use Dancer2;
>>> use Dancer2::Plugin::Menu;
>>>
>>> get '/' => sub { return template 'index' };
>>> get '/test' => sub {
>>> template 'index.tt', {
>>> html => 'hi',
>>> },
>>> };
>>> build_menu;
>>> }
>>>
>>> my $test = Plack::Test->create( TestApp->to_app );
>>> my $res;
>>>
>>> my $menu = Dancer2::Plugin::Menu->new(app => $test);
>>>
>>> # test the return value of plugin method
>>> my $result = $menu->_convert_routes_to_array;
>>>
>>> cmp_deeply($result, [ '/', '/test' ], 'returns array of menu paths');
>>>
>>> Is there a way to do this?
>>>
>>>
>>>
>>>
>>>
>>>
>>> --
>>> Prometheus Labor Communications, Inc.
>>> http://prometheuslabor.com
>>> 413-572-1300
>>>
>>> UnionConnect Phone App for Labor Unions
>>> http://unionconnect.com
>>>
>>
>>
>> --
>> Prometheus Labor Communications, Inc.
>> http://prometheuslabor.com
>> 413-572-1300
>>
>> UnionConnect Phone App for Labor Unions
>> http://unionconnect.com
>>
>
>
> --
> Prometheus Labor Communications, Inc.
> http://prometheuslabor.com
> 413-572-1300
>
> UnionConnect Phone App for Labor Unions
> http://unionconnect.com
>
--
Prometheus Labor Communications, Inc.
http://prometheuslabor.com
413-572-1300
UnionConnect Phone App for Labor Unions
http://unionconnect.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20181110/2b9dd0c8/attachment.html>
More information about the dancer-users
mailing list