Skip to content

Commit

Permalink
Merge pull request #16 from ziali088/master
Browse files Browse the repository at this point in the history
Add method to conveniently no-op
  • Loading branch information
geofffranks authored Jul 12, 2017
2 parents c698e79 + ed59ccc commit 66ef9a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/Test/MockModule.pm
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ sub mock {
}
}

sub noop {
my $self = shift;
$self->mock($_,1) for @_;
}

sub original {
my $self = shift;
my ($name) = @_;
Expand Down Expand Up @@ -327,6 +332,14 @@ Restores all the subroutines in the package that were mocked. This is
automatically called when all C<Test::MockObject> objects for the given package
go out of scope.
=item noop($subroutine [, ...])
Given a list of subroutine names, mocks each of them with a no-op subroutine. Handy
for mocking methods you want to ignore!
# Neuter a list of methods in one go
$module->noop('purge', 'updated');
=back
=head1 SEE ALSO
Expand Down
6 changes: 5 additions & 1 deletion t/lib/ExampleModule.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ sub param {
return;
}

1;
sub cookie {
return 'choc-chip';
}

1;
10 changes: 10 additions & 0 deletions t/mockmodule.t
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ like($@, qr/Invalid package name/, ' ... croaks if package is undefined');
$mcgi->mock('param', sub { return 'This sub is mocked' });
is(ExampleModule::param(), 'This sub is mocked', '... mocked params');
ok($mcgi->is_mocked('param'), '... returns true for non-mocked sub');

# noop()
is(ExampleModule::cookie(), 'choc-chip', 'cookie does default behaviour');
$mcgi->noop('cookie');
ok($mcgi->is_mocked('cookie'), 'cookie is mocked using noop');
$mcgi->unmock('cookie');
$mcgi->unmock('Vars');
$mcgi->noop('cookie', 'Vars');
is(ExampleModule::cookie(), 1, 'now cookie does nothing');
is(ExampleModule::Vars(), 1, 'now Vars does nothing');
}

isnt(ExampleModule::param(), 'This sub is mocked',
Expand Down

0 comments on commit 66ef9a3

Please sign in to comment.