| Test-Builder-Mock-Class documentation | Contained in the Test-Builder-Mock-Class distribution. |
Test::Builder::Mock::Class - Simulating other classes for Test::Builder
use Test::Builder::Mock::Class ':all';
use Test::More 'no_plan';
# concrete mock class
mock_class 'Net::FTP' => 'Net::FTP::Mock';
my $mock_object1 = Net::FTP::Mock->new;
$mock_object1->mock_tally;
# anonymous mocked class
my $metamock2 = mock_anon_class 'Net::FTP';
my $mock_object2 = $metamock2->new_object;
$mock_object2->mock_tally;
# anonymous class with role applied
my $metamock3 = Test::Builder::Mock::Class->create_mock_anon_class(
class => 'Net::FTP',
roles => [ 'My::Handler::Role' ],
);
my $mock_object3 = $metamock3->new_object;
$mock_object3->mock_tally;
This module adds support for standard Test::Builder framework (Test::Simple or Test::More) to Test::Mock::Class.
Mock class can be used to create mock objects which can simulate the behavior of complex, real (non-mock) objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test.
See Test::Mock::Class for more detailed documentation.
Creates the concrete mock class based on original class. If the name of
mock_class is undefined, its name is created based on name of original
class with added ::Mock suffix.
The function returns the metaclass object of new mock_class.
Creates an anonymous mock class based on original class. The name of this class is automatically generated. If class argument not defined, the empty mock class is created.
The function returns the metaobject of new mock class.
Imports all functions into caller's namespace.
The Test::Builder::Mock::Class fits perfectly to Test::Builder
(Test::Simple or Test::More) tests. It adds automatically the tests for
each mock_invoke (which is called implicitly by all mock methods) and
mock_tally. It means that you need to add these tests to your test plan.
Example code:
package My::ExampleTest;
use Test::More 'no_plan';
use Test::Builder::Mock::Class ':all';
require 'IO::File';
my $mock = mock_anon_class 'IO::File';
my $io = $mock->new_object;
$io->mock_return( open => 1, args => [qr//, 'r'] );
ok( $io->open('/etc/passwd', 'r'), '$io->open' );
$io->mock_tally;
Moose after version 1.05 calls BUILDALL method automatically, so this
is one more test to whole plan.
Test::More needs an exact count of all tests and it will be different for Moose before and after version 1.05. There are following workarounds:
# No plan at all use Test::More 'no_plan'; # Different plans depend on Moose version use Test::More; require Moose; plan tests => (Moose->VERSION >= 1.05 ? 10 : 8); # Require Moose >= 1.05 use Moose 1.05 (); use Test::More tests => 10;
Mock metaclass API: Test::Builder::Mock::Class::Role::Meta::Class, Moose::Meta::Class.
Mock object methods: Test::Builder::Mock::Class::Role::Object.
Perl standard testing: Test::Builder, Test::Simple, Test::More.
Mock classes for xUnit-like testing (Test::Unit::Lite): Test::Mock::Class.
Other implementations: Test::MockObject, Test::MockClass.
The API is not stable yet and can be changed in future.
Piotr Roszatycki <dexter@cpan.org>
Based on SimpleTest, an open source unit test framework for the PHP programming language, created by Marcus Baker, Jason Sweat, Travis Swicegood, Perrick Penet and Edward Z. Yang.
Copyright (c) 2009, 2010 Piotr Roszatycki <dexter@cpan.org>.
This program is free software; you can redistribute it and/or modify it under GNU Lesser General Public License.
| Test-Builder-Mock-Class documentation | Contained in the Test-Builder-Mock-Class distribution. |
#!/usr/bin/perl -c package Test::Builder::Mock::Class;
use 5.006; use strict; use warnings; our $VERSION = '0.0203'; use Moose;
extends 'Moose::Meta::Class';
with 'Test::Builder::Mock::Class::Role::Meta::Class'; use namespace::clean -except => 'meta';
BEGIN { my %exports = ();
$exports{mock_class} = sub {
sub ($;$) {
return __PACKAGE__->create_mock_class(
defined $_[1] ? $_[1] : $_[0] . '::Mock',
class => $_[0],
);
};
};
$exports{mock_anon_class} = sub {
sub (;$) {
return __PACKAGE__->create_mock_anon_class(
defined $_[0] ? (class => $_[0]) : (),
);
};
};
my %groups = ();
$groups{all} = [ keys %exports ];
require Sub::Exporter;
Sub::Exporter->import(
-setup => {
exports => [ %exports ],
groups => \%groups,
},
);
};
1;