View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Piotr Roszatycki > Test-Mock-Class-0.0303 > Test::Mock::Class



Annotate this POD


New  2
Open  0
View/Report Bugs
Module Version: 0.0303   Source  


Test::Mock::Class - Simulating other classes


  use Test::Mock::Class ':all';
  require Net::FTP;

  # concrete mocked class
  mock_class 'Net::FTP' => 'Net::FTP::Mock';
  my $mock_object = Net::FTP::Mock->new;

  # anonymous mocked class
  my $metamock = mock_anon_class 'Net::FTP';
  my $mock_object = $metamock->new_object;

  # anonymous class with role applied
  my $metamock = Test::Mock::Class->create_anon_class(
      roles => [ 'My::Handler::Role' ],
  my $mock_object = $metamock->new_object;


In a unit test, mock objects 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.

The unique features of Test::Mock::Class:



mock_class( class : Str, mock_class : Str = undef ) : Moose::Meta::Class

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 original class is loaded with Class::MOP::load_class function which behaves wrongly for some packages, i.e. IO::File. It is much safer to require original class explicitly.

The function returns the metaclass object of new mock_class.

mock_anon_class( class : Str = undef ) : Moose::Meta::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.


Test::Mock::Class ':all';

Imports all functions into caller's namespace.


The Test::Mock::Class fits perfectly to Test::Unit::Lite tests. It throws an exception immediately if some problem is occurred. It means that the test unit is failed if i.e. the mock method is called with wrong arguments.

Example code:

  package My::ExampleTest;

  use Test::Unit::Lite;

  use Moose;
  extends 'Test::Unit::TestCase';

  use Test::Assert ':all';
  use Test::Mock::Class ':all';

  require IO::File;

  sub test_mock_class {
      my ($self) = @_;

      my $mock = mock_anon_class 'IO::File';
      my $io = $mock->new_object;
      $io->mock_return( open => 1, args => [qr//, 'r'] );

      assert_true( $io->open('/etc/passwd', 'r') );



Mock metaclass API: Test::Mock::Class::Role::Meta::Class, Moose::Meta::Class.

Mock object methods: Test::Mock::Class::Role::Object.

xUnit-like testing: Test::Unit::Lite.

Mock classes for Test::Builder: Test::Builder::Mock::Class.

Other implementations: Test::MockObject, Test::MockClass.


The API is not stable yet and can be changed in future.


Piotr Roszatycki <>


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 <>.

This program is free software; you can redistribute it and/or modify it under GNU Lesser General Public License.

syntax highlighting: