mardi 5 juin 2018

perl: count calls to a method while testing

I've got a test that looks like this:

my $mockobj = My::Module->new();
$mockobj = Test::MockObject::Extends->new($obj);

my $callcount = 0;

$mockobj->mock('Foo', sub {
  print STDERR "foo\n";
  $callcount++;
  return 1;
});

$mockobj->CallsFoo();
is($callcount, 1);
$mockobj->CallsFooTwice();
is($callcount, 3);

I'm wondering if there's a better way to accomplish this. In some other languages, there are modules that are capable of monitoring the number of times a function is called (such as sinon for javascript). Is there such a thing for perl, or do I have to do it manually like this?

Aucun commentaire:

Enregistrer un commentaire