Test-Mocha

 view release on metacpan or  search on metacpan

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

# ABSTRACT: Mock objects
$Test::Mocha::Mock::VERSION = '0.67';
use parent 'Test::Mocha::SpyBase';
use strict;
use warnings;

use Test::Mocha::MethodCall;
use Test::Mocha::MethodStub;
use Test::Mocha::Util ();
use Types::Standard   ();
use if $] lt '5.025', 'UNIVERSAL::ref';

our $AUTOLOAD;

# Lookup table of classes for which mock isa() should return false
my %NOT_ISA =
  map { $_ => undef } ( 'Type::Tiny', 'Moose::Meta::TypeConstraint', );

# By default, isa(), DOES() and does() should return true for everything, and
# can() should return a reference to C<AUTOLOAD()> for all methods
my %DEFAULT_STUBS = (

lib/Test/Mocha/Spy.pm  view on Meta::CPAN

use parent 'Test::Mocha::SpyBase';
use strict;
use warnings;

use Carp 1.22 ();
use Scalar::Util ();
use Test::Mocha::MethodCall;
use Test::Mocha::MethodStub;
use Test::Mocha::Util ();
use Types::Standard   ();
use if $] lt '5.025', 'UNIVERSAL::ref';

our $AUTOLOAD;

my %DEFAULT_STUBS = (
    can => Test::Mocha::MethodStub->new(
        # can() should return a reference to AUTOLOAD() for all methods
        name      => 'can',
        args      => [Types::Standard::Str],
        responses => [
            sub {

t/mock_universal.t  view on Meta::CPAN

called_ok { $mock->can('foo') } '... and verified';

my $nr_calls = 1;
stub { $mock->ref } returns 'Foo';
is( $mock->__stubs->{ref}[0], 'ref()', 'ref() can be stubbed' );
is( $mock->ref,               'Foo',   '... and called as a method' );
is( ( my $call = ( inspect { $mock->ref } )[0] ), 'ref()',
    '... and inspected' );
SKIP: {
    skip 'UNIVERSAL::ref not compatible with Perl version >= 5.025', 1
      if $] ge '5.025';

    $nr_calls++;
    is( ref($mock), 'Foo', '... or called as a function (via UNIVERSAL::ref)' );
    my $call = ( inspect { ref($mock) } )[-1];
    is( $call, 'ref()', '... and inspected' );
   # Ensure UNIVERSAL::ref is not recorded as caller when it intercepts the call
    is( ( $call->caller )[0], __FILE__,
        '... and caller is not UNIVERSAL::ref' );
}
called_ok { $mock->ref } &times($nr_calls), '... and verified';

t/spy.t  view on Meta::CPAN

};

# ----------------------
# spy acts as a wrapper to the real object

ok( $spy->isa('TestClass'),  'spy isa(TestClass)' );
ok( $spy->DOES('TestClass'), 'spy DOES(TestClass)' );

SKIP: {
    skip 'UNIVERSAL::ref not compatible with Perl version >= 5.025', 1
      if $] ge '5.025';
    is( ref($spy), 'TestClass', 'ref(spy)' );
}
#iis( blessed($spy), 'TestClass' );

ok( !$spy->isa('Foo'),  'spy does not isa(Anything)' );
ok( !$spy->DOES('Baz'), 'spy does not DOES(Anything)' );

# ----------------------
# spy delegates method calls to the real object

t/spy_universal.t  view on Meta::CPAN


ok( $spy->can('get'), 'can() called' );
is( ( inspect { $spy->can('get') } )[0], 'can("get")', '... and inspected' );
called_ok { $spy->can('get') } '... and verified';

my $nr_calls = 1;
is( $spy->ref, 'TestClass', 'ref() called as a method' );
is( ( inspect { $spy->ref } )[0], 'ref()', '... and inspected' );
SKIP: {
    skip 'UNIVERSAL::ref not compatible with Perl version >= 5.025', 3
      if $] ge '5.025';

    $nr_calls++;
    is( ref($spy), 'TestClass',
        '... or called as a function (via UNIVERSAL::ref)' );
    my $call = ( inspect { ref($spy) } )[-1];
    is( $call, 'ref()', '... and inspected' );
   # Ensure UNIVERSAL::ref is not recorded as caller when it intercepts the call
    is( ( $call->caller )[0], __FILE__,
        '... and caller is not UNIVERSAL::ref' );
}



( run in 0.710 second using v1.01-cache-2.11-cpan-cc502c75498 )