[dancer-users] Hooks in dancer2

Rubén Amórtegui ruben.amortegui at gmail.com
Thu Oct 1 16:33:34 BST 2015


Hi all,

I have an issue executing hooks in dancer2, The code is running twice but
I'm just calling once.

This is a sample code (minimum)

File: lib/Dancer2/Plugin/MyPluginTest.pm
:::::::::::::::::::::::::
MyPluginTest.pm
:::::::::::::::::::::::::

package Dancer2::Plugin::MyPluginTest;
use Dancer2::Plugin;

register_hook 'my_new_hook_after';

register new_function => sub {
  my $dsl = shift;
 execute_hook 'my_new_hook_after';
};

register_plugin;

:::::::::::::::::::::::::


Test File: plugin.pl
::::::::::::::
plugin.pl
::::::::::::::
use strict;
use warnings;

use Test::More;
use Plack::Test;
use HTTP::Request::Common;
{
    package MyTestApp;
    use Dancer2;
    use Dancer2::Plugin::MyPluginTest;

    my $var = 0;

    get '/' => sub {
      new_function;
      $var;
    };

    hook 'my_new_hook_after' => sub {
      $var++;
      debug "Executing hook -  $var";
      return;
    };
};

my $test = Plack::Test->create( MyTestApp->to_app );

my $res  = $test->request( GET '/' );
is( $res->content, '1', 'Var is 1 after one call on  /' );
$res  = $test->request( GET '/' );
is( $res->content, '2', 'Var is 1 after one call on  /' );
done_testing();
::::::::::::::


The result after : prove -l lib/ plugin.pl -v


plugin.pl .. [MyTestApp:14688] debug @2015-10-01 09:25:43> Executing hook
-  1 in plugin.pl l. 21
[MyTestApp:14688] debug @2015-10-01 09:25:43> Executing hook -  2 in
plugin.pl l. 21

not ok 1 - Var is 1 after one call on  /

#   Failed test 'Var is 1 after one call on  /'
#   at plugin.pl line 28.
#          got: '2'
#     expected: '1'
[MyTestApp:14688] debug @2015-10-01 09:25:43> Executing hook -  3 in
plugin.pl l. 21
[MyTestApp:14688] debug @2015-10-01 09:25:43> Executing hook -  4 in
plugin.pl l. 21
not ok 2 - Var is 1 after one call on  /

#   Failed test 'Var is 1 after one call on  /'
#   at plugin.pl line 30.
#          got: '4'
#     expected: '2'
1..2
# Looks like you failed 2 tests of 2.
Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/2 subtests

Test Summary Report
-------------------
plugin.pl (Wstat: 512 Tests: 2 Failed: 2)
  Failed tests:  1-2
  Non-zero exit status: 2
Files=1, Tests=2,  2 wallclock secs ( 0.04 usr  0.00 sys +  0.44 cusr  0.02
csys =  0.50 CPU)
Result: FAIL


Does someone an idea what i'm doing wrong? or if is it something about the
hooks?

Thanks in advance!


Ruben.

-- 
____________________________
Rubén Darío Amórtegui Medina
MCE - Ingeniero de Sistemas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20151001/8bd401eb/attachment.html>


More information about the dancer-users mailing list