This is the mock module I’m using:
http://metacpan.org/pod/Test::MockModule
How to mock sub a to sub b,
where sub b just does something else before call sub a?
sub b {
#do something else
a(@_);
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can grab the un-mocked method with
can(UNIVERSAL::can). After that you can eithergotoit or just use the ampersand calling style to pass the same arguments. That’s what I did below.This of course assumes that your sub isn’t
AUTOLOADor generated throughAUTOLOADwithout redefiningcan. (I learned years back that if you’re going to mess withAUTOLOAD, it’s probably best to do the work incan.)You could also create your own utility that does this automatically, by
invadingmodifying theTest::MockModule‘s namespace.And you could call it like so: