The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::AutoMock::Patch - Monkey patch for returning AutoMock

SYNOPSIS

  use Test::AutoMock::Patch qw(patch_sub);

  # a black box function you want to test
  sub get_metacpan {
      my $ua = LWP::UserAgent->new;
      my $response = $ua->get('https://metacpan.org/');
      if ($response->is_success) {
          return $response->decoded_content;  # or whatever
      }
      else {
          die $response->status_line;
      }
  }

  # apply a monkey patch to LWP::UserAgent::new
  patch_sub {
      my $mock = shift;

      # set up the mock
      manager($mock)->add_method('get->decoded_content' => "Hello, metacpan!\n");

      # call blackbox function
      my $body = get_metacpan();

      # assertions
      is $body, "Hello, metacpan!\n";
      manager($mock)->called_with_ok('get->is_success' => []);
      manager($mock)->not_called_ok('get->status_line');
  } 'LWP::UserAgent::new';

DESCRIPTION

Temporarily replace any subroutine and return AutoMock. It is convenient when mock can not be injected from outside.

FUNCTIONS

patch_sub

    patch_sub {
        my ($mock, $other_mock) = @_;

        # write your test using $mock

    } 'Path::To::subroutine', 'Path::To::other_subroutine';

Replace the specified subroutine with one that returns a mock, and execute the code in the block. The mock object is passed as the argument of the block by the number of replaced subroutines. After exiting the block, the patch is removed.

The generated mock object is an instance of Test::AutoMock::Mock::Overloaded.

It is a common usage to patch the class method used as a constructor.

LICENSE

Copyright (C) Masahiro Honma.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Masahiro Honma <hiratara@cpan.org>