Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to call orig subroutine when we override it #944

Open
KES777 opened this issue Mar 18, 2022 · 2 comments
Open

Add ability to call orig subroutine when we override it #944

KES777 opened this issue Mar 18, 2022 · 2 comments

Comments

@KES777
Copy link

KES777 commented Mar 18, 2022

We need some functionality similar to around. This could be useful when we override only particular calls

@exodist
Copy link
Member

exodist commented Mar 18, 2022

You can always get the origjnal from the $mock object: https://metacpan.org/pod/Test2::Mock#$mock-%3Eorig($SYMBOL)

Also there is an 'around' method to Test2::Mock, and that is usable in Test2::Tools::Mock which is just candy around it:
https://metacpan.org/pod/Test2::Mock#$mock-%3Earound($NAME,-sub-{-...-})

So this works fine:

use Test2::V0;

{
    package Foo;

    sub blah { 'blah' }
}

my $mock = mock 'Foo' => (
    around => [
        blah => sub {
            my ($orig, $class, @args) = @_; 

            my $got = $class->$orig(@args);

            return "around $got";
        },  
    ]   
);

is(Foo->blah, "around blah", "Mocked foo() and called original inside it");

done_testing;
ok 1 - Mocked foo() and called original inside it
1..1

So maybe the problem is we need more documentation?

@KES777
Copy link
Author

KES777 commented Mar 18, 2022

yes, would be nice to know list of methods which could be used like override and add. And now I know that around could be used too.

@exodist exodist transferred this issue from Test-More/Test2-Suite Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants