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 a function to automatically mock all subroutine with a no-op or die function. #30

Open
shinglyu opened this issue Aug 13, 2018 · 3 comments

Comments

@shinglyu
Copy link

Sometimes when I mock I forgot to mock some subroutine and the unit test calls the real module, causing hard-to-debug tests. It would be nice to have a function that can automatically mock all subroutines of a module with a no-op implementation or sub { die }, so if we forgot to mock a subroutine, the whole test will fail early.

Something like

my $module = Test::MockModule->new('Foo');
$module->auto_mock_all();

Foo->bar(); # dies, or simply no-op
@shinglyu
Copy link
Author

I'm trying to implement this but I'm stuck at how to get all subroutines of a module, I tried keys %Module:: but sometimes I got things that is not a function. Any tips?

@toddr
Copy link
Contributor

toddr commented Aug 17, 2018

A Example of getting the subs out of Safe might be:

perl -MSafe -E'my @subs = grep { Safe->can($_) } keys %Safe::; say foreach @subs'

@toddr
Copy link
Contributor

toddr commented Aug 17, 2018

YMMV on inheritance. Just the same I like the idea!

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