The mocking call of a super class in Perl (using Test :: MockObject)

I use MockObjects in some of my tests and just had to test the function with the SUPER class call, and I can't get it to work. Can UNIVERSAL call $ this-> SUPER :: save () not to mock? If so, how do you do it?

Thanks.

Edit:

Found!

Use fake_module from Test::MockObject

So, let your base module be Some::Module , and your routine will call $this->SUPER::save , use

 my $child_class_mockup = Test::MockObject->new(); $child_class_mockup->fake_module( 'Some::Module', save => sub () { return 1; } ); 

Leaving the question open for a couple of days to get information about the various methods / libraries for this action (what if the SUPER call had a SUPER call?) Before accepting this answer.

+7
source share
1 answer

Find out the name of the object's superclass (or one of the superclasses, since Perl has multiple inheritance) and define the save call in the superclass package.

For example, if you have

 package MyClass; use YourClass; our @ISA = qw(YourClass); # <-- name of superclass ... sub foo { my $self = shift; ... $self->SUPER::save(); # <--- want to mock this function in the test ... } sub save { # MyClass version of save method ... } 

then in your test script you would say

 no warnings 'redefine'; # optional, suppresses warning sub YourClass::save { # mock function for $yourClassObj->save, but also # a mock function for $myClassObj->SUPER::save ... } 
+2
source

All Articles