|
NAMEMock::Quick - Quickly mock objects and classes, even temporarily replace them, side-effect free.DESCRIPTIONMock-Quick is here to solve the current problems with Mocking libraries.There are a couple Mocking libraries available on CPAN. The primary problems with these libraries include verbose syntax, and most importantly side-effects. Some Mocking libraries expect you to mock a specific class, and will unload it then redefine it. This is particularly a problem if you only want to override a class on a lexical level. Mock-Quick provides a declarative mocking interface that results in a very concise, but clear syntax. There are separate facilities for mocking object instances, and classes. You can quickly create an instance of an object with custom attributes and methods. You can also quickly create an anonymous class, optionally inheriting from another, with whatever methods you desire. Mock-Quick also provides a tool that provides an OO interface to overriding methods in existing classes. This tool also allows for the restoration of the original class methods. Best of all this is a localized tool, when your control object falls out of scope the original class is restored. SYNOPSISMOCKING OBJECTSuse Mock::Quick; my $obj = qobj( foo => 'bar', # define attribute do_it => qmeth { ... }, # define method ... ); is( $obj->foo, 'bar' ); $obj->foo( 'baz' ); is( $obj->foo, 'baz' ); $obj->do_it(); # define the new attribute automatically $obj->bar( 'xxx' ); # define a new method on the fly $obj->baz( qmeth { ... }); # remove an attribute or method $obj->baz( qclear() ); STRICTER MOCKuse Mock::Quick; my $obj = qstrict( foo => 'bar', # define attribute do_it => qmeth { ... }, # define method ... ); is( $obj->foo, 'bar' ); $obj->foo( 'baz' ); is( $obj->foo, 'baz' ); $obj->do_it(); # remove an attribute or method $obj->baz( qclear() ); You can no longer auto-vivify accessors and methods in strict mode: # Cannot define the new attribute automatically dies_ok { $obj->bar( 'xxx' ) }; # Cannot define a new method on the fly dies_ok { $obj->baz( qmeth { ... }) }; In order to add methods/accessors you need to create a control object. CONTROL OBJECTSControl objects are objects that let you interface a mocked object. They let you add attributes and methods, or even clear them. This is unnecessary unless you use strict mocking, or choose not to import qmeth() and qclear().
MOCKING CLASSESNote: the control object returned here is of type Mock::Quick::Class, whereas control objects for qobj style objects are of Mock::Quick::Object::Control.IMPLEMENT A CLASS 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; my $control = qclass( -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 qimplement() method instead of qclass: use Mock::Quick; my $control = qimplement 'Some::Package' => ( %args ); ANONYMOUS MOCKED CLASS 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; my $control = qclass( # 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(); TAKING OVER EXISTING/LOADED CLASSES use Mock::Quick; my $control = qtakeover 'Some::Package' => ( %overrides ); # 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 qclass(): use Mock::Quick; my $control = qclass( -takeover => 'Some::Package', %overrides ); METRICSAll control objects have a 'metrics' method. The metrics method 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.Metrics only apply to mocked methods. When you takeover an already loaded class metrics will only track overridden methods. EXPORTSMock-Quick uses Exporter::Declare. This allows for exports to be prefixed or renamed. See "RENAMING IMPORTED ITEMS" in Exporter::Declare for more information.
AUTHORSChad Granum exodist7@gmail.comBen Hengst notbenh@cpan.org CONTRIBUTORSContributors are listed as authors in modules they have touched.
COPYRIGHTCopyright (C) 2011 Chad GranumMock-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.
Visit the GSP FreeBSD Man Page Interface. |