One approach is to replace the function in Foo before Bar is compiled.
# Compile foo use Foo; # Replace (mock) a function in Foo BEGIN { no warnings 'redefine'; *Foo::get_db_value = sub { return 2 } } # Compilation of Bar finds mocked function, not original function # (Foo was already loaded so is not recompiled) use Bar; # Test function is(Bar::bar_func(), 84, "bar_func works as advertised");
You could replace the "2" with a package variable (or package-scoped lexical) if you want to be able to change the value returned for use in different tests.
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
In reply to Re: Testing procedure: how to mock?
by xdg
in thread Testing procedure: how to mock?
by thor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |