jeudi 17 décembre 2015

Test::MockTime does not mock time in certain cases

I have a very simple script which mocks time using Test::MockTime, but the output of time call is different in two parts of the code. Here is the script:

package mocker;

use strict;
use warnings;

sub abcd {
    print "in abcd, time is " . time . "\n";
}

BEGIN {
    use Test::MockTime qw(set_absolute_time restore_time);
    set_absolute_time(0);
};

sub do_mock {
    print "Current epoch in do_mock is: " . time . "\n";
    abcd;
    restore_time();
}

1;

I call mocker::do_mock in my script:

use strict;
use warnings;

use mocker;

mocker::do_mock;

I expect output to have "0" as current time in both print statements but strangely I have this output:

Current epoch in do_mock is: 0
in abcd, time is 1450343385

So, WHY in abcd time is restored to current time?

Aucun commentaire:

Enregistrer un commentaire