GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Mock::Quick::Class(3) User Contributed Perl Documentation Mock::Quick::Class(3)

Mock::Quick::Class - Class mocking for Mock::Quick

Provides class mocking for Mock::Quick

This will implement a class at the namespace provided via the -implement argument. The class must not already be loaded. Once complete the real class will be prevented from loading until you call undefine() on the control object.

    use Mock::Quick::Class;

    my $control = Mock::Quick::Class->new(
        -implement => 'My::Package',

        # Insert a generic new() method (blessed hash)
        -with_new => 1,

        # Inheritance
        -subclass => 'Some::Class',
        # Can also do
        -subclass => [ 'Class::A', 'Class::B' ],

        # generic get/set attribute methods.
        -attributes => [ qw/a b c d/ ],

        # Method that simply returns a value.
        simple => 'value',

        # Custom method.
        method => sub { ... },
    );

    my $obj = $control->package->new;
    # OR
    my $obj = My::Package->new;

    # Override a method
    $control->override( foo => sub { ... });

    # Restore it to the original
    $control->restore( 'foo' );

    # Remove the namespace we created, which would allow the real thing to load
    # in a require or use statement.
    $control->undefine();

You can also use the 'implement' method instead of new:

    use Mock::Quick::Class;

    my $control = Mock::Quick::Class->implement(
        'Some::Package',
        %args
    );

This is if you just need to generate a class where the package name does not matter. This is done when the -takeover and -implement arguments are both omitted.

    use Mock::Quick::Class;

    my $control = Mock::Quick::Class->new(
        # Insert a generic new() method (blessed hash)
        -with_new => 1,

        # Inheritance
        -subclass => 'Some::Class',
        # Can also do
        -subclass => [ 'Class::A', 'Class::B' ],

        # generic get/set attribute methods.
        -attributes => [ qw/a b c d/ ],

        # Method that simply returns a value.
        simple => 'value',

        # Custom method.
        method => sub { ... },
    );

    my $obj = $control->package->new;

    # Override a method
    $control->override( foo => sub { ... });

    # Restore it to the original
    $control->restore( 'foo' );

    # Remove the anonymous namespace we created.
    $control->undefine();

    use Mock::Quick::Class;

    my $control = Mock::Quick::Class->takeover( 'Some::Package' );

    # Override a method
    $control->override( foo => sub { ... });

    # Restore it to the original
    $control->restore( 'foo' );

    # Destroy the control object and completely restore the original class
    # Some::Package.
    $control = undef;

You can also do this through new()

    use Mock::Quick::Class;

    my $control = Mock::Quick::Class->new(
        -takeover => 'Some::Package',
        %overrides
    );

While the control object exists, it can be accessed via "YOUR::PACKAGE-"MQ_CONTROL()>. It is important to note that this method will disappear whenever the control object you track falls out of scope.

Example (taken from Class.t):

    $obj = $CLASS->new( -takeover => 'Baz' );
    $obj->override( 'foo', sub {
        my $class = shift;
        return "PREFIX: " . $class->MQ_CONTROL->original( 'foo' )->();
    });

    is( Baz->foo, "PREFIX: foo", "Override and accessed original through MQ_CONTROL" );
    $obj = undef;

    is( Baz->foo, 'foo', 'original' );
    ok( !Baz->can('MQ_CONTROL'), "Removed control" );

$package = $obj->package()
Get the name of the package controlled by this object.
$bool = $obj->is_takeover()
Check if the control object was created to takeover an existing class.
$bool = $obj->is_implement()
Check if the control object was created to implement a class.
$data = $obj->metrics()
Returns a hash where keys are method names, and values are the number of times the method has been called. When a method is altered or removed the key is deleted.
$obj->override( name => sub { ... })
Override a method.
$obj->original( $name );
Get the original method (coderef). Note: The first time this is called it find and remembers the value of package->can( $name ). This means that if you modify or replace the method without using Mock::Quick before this is called, it will have the updated method, not the true original.

The override() method will call this first to ensure the original method is cached and available for restore(). Once a value is set it is never replaced or cleared.

$obj->restore( $name )
Restore a method (Resets metrics)
$obj->undefine()
Undefine the package controlled by the control.

Chad Granum exodist7@gmail.com
Glen Hinkle glen@empireenterprises.com

Copyright (C) 2011 Chad Granum

Mock-Quick is free software; Standard perl licence.

Mock-Quick is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.

2022-04-08 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.