Fork perl mockmodule

I am trying to make fun of a fork call using mockmodule. I set it up like this ...

my $mock = Test::MockModule->new('Foo');
$modMock->mock(fork => sub { print "here"; return 0; });

where is my module foo.pm. I had experience working with other module calls loaded into the module under test, and mocking module calls like this seem to work well. However, my breadboard print application is never reached (and the real fork is called).

Is this the right way to mock system calls like fork? Should I load a different module than the system under test?

+3
source share
1 answer

&Foo::fork, , Foo::fork() sigil &fork, Foo.

package Foo;
TestModule->new('Foo')->mock(fork => sub { ... });

Foo::fork;             # calls mocked function
⋔                 # calls mocked function
{ package Bar; &fork } # error: no &Bar::fork
fork;                  # calls builtin

, fork , " ". , , subs :

package Foo;
use subs 'fork'; # compile-time import of name 'fork'
TestModule->new('Foo')->mock(fork => sub { ... });

Foo::fork;             # calls mocked function
⋔                 # calls mocked function
fork;                  # now calls mocked function
{ package Bar; fork; } # calls builtin 
CORE::fork;            # always calls builtin
+4

All Articles