Test-Mock-Simple

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for Perl extension Test::Mock::Simple.

0.10 Sat Jan 25 16:50:00 2019
    - added module_location
      This change fixes an old bug report that I didn't think could be solved.
      https://rt.cpan.org/Public/Bug/Display.html?id=95826
      (Better late than never!)
      This allows you to tell Test::Mock::Simple where to find the modules if
      the namespace does not translate into the file that it is located in.

0.09 Mon Apr  6 14:16:00 2015
    - add no_load option

0.081 Thu Dec 18 14:07:00 2014
    - fix tarball - used Mac's broken tar to create the tardist

0.08  Sat Dec  7 08:39:00 2014
    - Removed my debugging module which I accidentally deployed

0.07  Sat Dec  6 20:48:00 2014
	- Added 'allow_new_methods' flag
      This breaks backwards compatibility, however, the arguments for including
      this flag have been persuasive enough for me to break it.
      Hopefully, this only creates a small headache for anyone using Test::Mock::Simple.

0.06  Sun Aug 31 21:27:00 2014
	- Rewrote Failures without Test::Exception

0.05  Fri Aug 29 21:07:00 2014
	- Added Failures test and cleaned up serveral bugs

0.04  Wed Jan 10 09:28:00 2014
	- Added TEST_MOCK_SIMPLE_DISABLE

0.03  Wed Oct 11 11:08:00 2013
	- updated (downgraded) perl version in Makefile.PL

0.02  Wed Oct  9 10:46:00 2013
	- changed method 'it' to 'add'
	- lowered required perl method to 5.8.8

0.01  Fri Oct  4 10:21:24 2013
	- original version; created by h2xs 1.23 with options
		-AXn Test::Mock::Simple

Makefile.PL  view on Meta::CPAN

use 5.008008;
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
    NAME              => 'Test::Mock::Simple',
    VERSION_FROM      => 'lib/Test/Mock/Simple.pm', # finds $VERSION
    PREREQ_PM         => {}, # e.g., Module::Name => 1.1
    ($] >= 5.005 ?     ## Add these new keywords supported since 5.005
      (ABSTRACT_FROM  => 'lib/Test/Mock/Simple.pm', # retrieve abstract from module
       AUTHOR         => 'Erik Tank <tank@jundy.com>') : ()),
    ($ExtUtils::MakeMaker::VERSION >= 6.3002 ? ('LICENSE'  => 'perl', ) : ()),
    (eval { ExtUtils::MakeMaker->VERSION(6.46) } ? (META_MERGE => {
    'meta-spec' => { version => 2 },
     resources => {
         repository => {

README.md  view on Meta::CPAN

# NAME

Test::Mock::Simple - A simple way to mock out parts of or a whole module.

# SYNOPSIS

```perl5
    use Test::Mock::Simple;

    my $total = 0;

    # Original::Module has methods increase, decrease, and sum
    my $mock = Test::Mock::Simple->new(module => 'Original::Module');
    $mock->add(increase => sub { shift; return $total += shift; });
    $mock->add(decrease => sub { shift; return $total -= shift; });

    my $obj = Original::Module->new();
    $obj->increase(5);
    $obj->decrease(2);
    print $obj->sum . "\n"; # prints 3
```

# DESCRIPTION

README.md  view on Meta::CPAN


        module\_location expects a PATHNAME to the file (relative to the @INC paths) which
        contains the namespace (or module) that you want to mock.

        This is useful when a single file declares multiple namespaces or in the event of bad
        coding where the module's namespace does not map to the module's location.

        Example:

        ```perl5
        use Test::Mock::Simple;

        my $mock = Test::Mock::Simple->new(
          module          => 'Original::Module',
          module_location => 'Modules/Orignal/Module.pm',
        );
        ```

    - allow\_new\_methods

        To create methods that do not exist in the module that is being mocked.

        The default behavior is to not allow adding methods that do not exist.  This

lib/Test/Mock/Simple.pm  view on Meta::CPAN

package Test::Mock::Simple;

use 5.008008;
use strict;
use warnings;

our $VERSION = '0.10';

my $allow_new_methods = 0;

sub new {

lib/Test/Mock/Simple.pm  view on Meta::CPAN


    *{$self->{module} . '::' . $name} = $sub;
  }
}

1;
__END__

=head1 NAME

Test::Mock::Simple - A simple way to mock out parts of or a whole module.

=head1 SYNOPSIS

  use Test::Mock::Simple;

  my $total = 0;

  # Original::Module has methods increase, decrease, and sum
  my $mock = Test::Mock::Simple->new(module => 'Original::Module');
  $mock->add(increase => sub { shift; return $total += shift; });
  $mock->add(decrease => sub { shift; return $total -= shift; });

  my $obj = Original::Module->new();
  $obj->increase(5);
  $obj->decrease(2);
  print $obj->sum . "\n"; # prints 3

=head1 DESCRIPTION

lib/Test/Mock/Simple.pm  view on Meta::CPAN

=item module_location

module_location expects a PATHNAME to the file (relative to the @INC paths) which
contains the namespace (or module) that you want to mock.

This is useful when a single file declares multiple namespaces or in the event of bad
coding where the module's namespace does not map to the module's location.

Example:

  use Test::Mock::Simple;

  my $mock = Test::Mock::Simple->new(
    module          => 'Original::Module',
    module_location => 'Modules/Orignal/Module.pm',
  );

=back

=over 4

=item allow_new_methods

t/Failures.t  view on Meta::CPAN


# change 'tests => 1' to 'tests => last_test_to_print';

use strict;
use warnings;
use lib 't/';

use Test::More tests => 6;

BEGIN {
  use_ok('Test::Mock::Simple');
}

#########################

# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.

eval { my $mock = Test::Mock::Simple->new(); };
like($@, qr/^No module name provided/, 'new with no args');

my $mock = Test::Mock::Simple->new(module => 'TestModule');
my $mock2 = $mock->new(module => 'TestModule');

eval { $mock->add(); };
like($@, qr/^No method name provided/, 'add with no args');

eval { $mock->add( 'sub' ); };
like($@, qr/^No sub ref provided/, 'add with no sub ref provided');

eval { require Mock::StrictModule; };
like($@, qr/^Module \(StrictModule\) does not have a method named 'add'/,

t/Mock/StrictModule.pm  view on Meta::CPAN

package Mock::StrictModule;

use strict;
use warnings;

use Test::Mock::Simple;

my $mock = Test::Mock::Simple->new(module => 'StrictModule');

$mock->add(add => sub { return "Should have die'd with a error"; });

1;

t/Mock/TestModule.pm  view on Meta::CPAN

package Mock::TestModule;

use strict;
use warnings;

use Test::Mock::Simple;

my $mock = Test::Mock::Simple->new(
  allow_new_methods => 1,
  module            => 'TestModule'
);
$mock->add(one     => sub { return 'eins';                   });
$mock->add(rooster => sub { return 'kikeriki';               });
$mock->add(add     => sub { return 'No namespace conflicts'; });

1;

t/Test-Mock-Simple.t  view on Meta::CPAN


# change 'tests => 1' to 'tests => last_test_to_print';

use strict;
use warnings;
use lib 't/';

use Test::More tests => 7;

BEGIN {
  use_ok('Test::Mock::Simple');
  use_ok('Mock::TestModule');
}

#########################

# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.

my $test = TestModule->new();

t/hidden_namespace.t  view on Meta::CPAN

use strict;
use warnings;
use lib 't/';

use Test::More tests => 5;

BEGIN {
  use_ok('Test::Mock::Simple');
}

my $mock = Test::Mock::Simple->new(
    allow_new_methods => 1,
    module            => 'Namespace::Within',
    module_location   => 'TestModule.pm',
);
$mock->add(bar => sub { return 'foo'; });

my $test = Namespace::Within->new();

ok($test->can('foo'), 'Module is able to call method foo');
ok($test->can('bar'), 'Module is able to call method bar');



( run in 2.362 seconds using v1.01-cache-2.11-cpan-71847e10f99 )