File Coverage

blib/lib/Test/Magpie/Inspect.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Test::Magpie::Inspect;
2             {
3             $Test::Magpie::Inspect::VERSION = '0.11';
4             }
5             # ABSTRACT: Inspect method invocations on mock objects
6              
7 6     6   5878 use Moose;
  0            
  0            
8             use namespace::autoclean;
9              
10             use aliased 'Test::Magpie::Invocation';
11              
12             use List::Util qw( first );
13             use Test::Magpie::Util qw( extract_method_name get_attribute_value );
14              
15             with 'Test::Magpie::Role::HasMock';
16              
17             our $AUTOLOAD;
18              
19             sub AUTOLOAD {
20             my $self = shift;
21              
22             my $inspect = Invocation->new(
23             method_name => extract_method_name($AUTOLOAD),
24             arguments => \@_,
25             );
26              
27             my $mock = get_attribute_value($self, 'mock');
28             my $invocations = get_attribute_value($mock, 'invocations');
29              
30             return first { $inspect->satisfied_by($_) } @$invocations;
31             }
32              
33             __PACKAGE__->meta->make_immutable;
34             1;
35              
36             __END__
37              
38             =pod
39              
40             =encoding utf-8
41              
42             =head1 NAME
43              
44             Test::Magpie::Inspect - Inspect method invocations on mock objects
45              
46             =head1 SYNOPSIS
47              
48             my $mock = mock;
49             $mock->something({ deep => { structure => [] }};
50             my $invocation = inspect($mock)->something(anything);
51             ok(defined $invocation, 'something was called');
52             is_deeply(($invocation->arguments)[0],
53             { deep => { structure => [] }})
54              
55             =head1 DESCRIPTION
56              
57             Inspecting a mock object allows you to write slightly clearer tests than having
58             a complex verification call.
59              
60             L<Test::Magpie/inspect> gives back an object of this class that has the same
61             API as your mock object. When a method is called, it checks if any invocation
62             matches its name and argument specification (inspectors can use argument
63             matchers). If so it will return that invocation as a L<Test::Magpie::Invocation>
64             object. Otherwise, C<undef> is returned.
65              
66             =head1 AUTHORS
67              
68             =over 4
69              
70             =item *
71              
72             Oliver Charles <oliver.g.charles@googlemail.com>
73              
74             =item *
75              
76             Steven Lee <stevenwh.lee@gmail.com>
77              
78             =back
79              
80             =head1 COPYRIGHT AND LICENSE
81              
82             This software is copyright (c) 2013 by Oliver Charles.
83              
84             This is free software; you can redistribute it and/or modify it under
85             the same terms as the Perl 5 programming language system itself.
86              
87             =cut