ePages 7.23.0 - DE_EPAGES/Test/Mock/Mockify.pm

Package DE_EPAGES::Test::Mock::Mockify

frame work to create stable mocks/stubs

Example
use DE_EPAGES::Test::Mock::Mockify;
my $Mockify = DE_EPAGES::Test::Mock::Mockify->new(
    'DE_EPAGES::Test::API::ModuleTwo',
    $aParameterList
);
my $MethodName = 'doWebservice';
my $ReturnValue = 'TestReturnValue';
my $aParameterTypes = [{'string' => 'aValue'}];
$Mockify->addMockWithReturnValueAndParameterCheck($MethodName, $ReturnValue, $aParameterTypes);
my $MockedModuleTwo = $Mockify->getMockObject();
my $ModuleOne = DE_EPAGES::Test::API::ModuleOne->new($MockedModuleTwo);
is( $ModuleOne->doSomeThingWithModuleTwo('aValue'), $ReturnValue, "$SubTestName - test if all is fine" );
@EXPORT_OK
GetParametersFromMockifyCall
WasCalled
GetCallCount

Functions

GetCallCount
GetParametersFromMockifyCall
WasCalled
addMethodSpy
addMethodSpyWithParameterCheck
addMock
addMockWithConditionalReturn
addMockWithReturnValue
addMockWithReturnValueAndParameterCheck
getMockObject
mock
new

GetCallCount

Return a number with the amount of calls, this mockified method was called. Dies if the requested method was not Mockified.

Syntax
GetCallCount( $MockifiedMockedObject, $MethodName );
Input
$MockifiedMockedObject (object)
MockifiedMockedObject which was build with Mockify
$MethodName (string)
name of method
Return
(integer)
Amount Of Calls

GetParametersFromMockifyCall

After mocking a method with Mockify framework and using the method, you can use this function to retrive you the Parameters that were given to the mocked method. If the Mocked Method was called mutiple times you can assess the parameters from a specific call. If Position is not defined it tocks the parameters from the first call. The position acts like an Array so first Element you will get with 0, the second with 1, and so on.

Syntax
GetParametersFromMockifyCall( );
Input
$MockifiedMockedObject (object)
MockifiedMockedObject which was build with Mockify
$MethodName (string)
name of method
$Position (Input1Type_boolean_integer_String_object_ref_hash_refarray)
Return
(array)
Array with the parameters that were given to the mocked method

WasCalled

returns true if the Method was called

Syntax
WasCalled( $MockifiedMockedObject, $MethodName );
Input
$MockifiedMockedObject (object)
MockifiedMockedObject which was build with Mockify
$MethodName (string)
name of method
Return
$WasCalled (boolean)
was called

addMethodSpy

Give option to observe a Method while keeping the original functionality.

Syntax
addMethodSpy( $MethodName );
Input
$MethodName (string)
$MethodName

addMethodSpyWithParameterCheck

Give option to observe a Method while keeping the original functionality. but also check the parameters check, based on the $aParameterTypes if it was called with the correct parameters my $aParameterTypes = ['string',{'string' => 'ABCD'}]; $Mockify->addMockWithReturnValueAndParameterCheck('myMethod','the return value',$aParameterTypes); my $MyFakeObject = $MockObject->getMockObject(); ok( $MyModuleObject->myMethod('Hello','ABCD') ), possible parameters types are: ['string', 'int', 'hashref', 'arrayref', 'object', 'undef', 'any'] or with more detail: [{'string'=>'abcdef'}, {'int' => 123}, {'hashref' => {'key'=>'value'}}, {'arrayref'=>['one', 'two']}, {'object'=> 'PAth::to:Obejct}]

Syntax
addMethodSpyWithParameterCheck( $MethodName, $aParameterTypes );
Input
$MethodName (String)
name of method
$aParameterTypes (refarray)
differnd parameter types

addMock

overrides a method in the MockedModule. Throws Error if method not exists in the MockedModule. $MockObject->addMock('myMethod', sub { # Your implementation } );

Syntax
addMock( $MethodName, $rSub );
Input
$MethodName (String)
name of method
$rSub (refSub)
function pointer which will be used for overriding

addMockWithConditionalReturn

will add a Method to the mocked module will return the $ReturnValue if it was called with the matching parameters. can be called multiple times to implement different return values for different parameters. my $aParameterMatchers = ['string',{'string' => 'ABCD'}]; my $aParameterMatchers2 = ['string',{'string' => 'EFGH'}]; $Mockify->addMockWithConditionalReturn('myMethod','the return value',$aParameterMatchers); $Mockify->addMockWithConditionalReturn('myMethod','the second return value',$aParameterMatchers2); my $MyFakeObject = $MockObject->getMockObject(); $MyModuleObject->myMethod('Hello','ABCD'); #will return 'the return value' $MyModuleObject->myMethod('Hello','EFGH'); #will return 'the second return value' possible parameters types are: ['string', 'int', 'hashref', 'arrayref', 'object', 'undef', 'any'] or with more detail: [{'string'=>'abcdef'}, {'int' => 123}, {'hashref' => {'key'=>'value'}}, {'arrayref'=>['one', 'two']}, {'object'=> 'PAth::to:Obejct}]

Syntax
addMockWithConditionalReturn( $MethodName, $ReturnValue, $aParameterMatchers );
Input
$MethodName (String)
name of method
$ReturnValue (integer String object refhash refarray)
value which will be returned by the mocked method if the parameters match the matchers
$aParameterMatchers (refarray)
differnd parameter types

addMockWithReturnValue

add a Method to the mocked module which always returns the $ReturnValue. The added Method will throw an Error if it was called with a Parameter. $Mockify->addMockWithReturnValue('myMethod','the return value');

Syntax
addMockWithReturnValue( $MethodName, $ReturnValue );
Input
$MethodName (String)
name of method
$ReturnValue (integer String object refhash refarray)
value which will be returned by the mocked method

addMockWithReturnValueAndParameterCheck

will add a Method to the mocked module will return the $ReturnValue check, based on the $aParameterTypes if it was called with the correct parameters my $aParameterTypes = ['string',{'string' => 'ABCD'}]; $Mockify->addMockWithReturnValueAndParameterCheck('myMethod','the return value',$aParameterTypes); my $MyFakeObject = $MockObject->getMockObject(); ok( $MyModuleObject->myMethod('Hello','ABCD') ), possible parameters types are: ['string', 'int', 'hashref', 'arrayref', 'object', 'undef', 'any'] or with more detail: [{'string'=>'abcdef'}, {'int' => 123}, {'hashref' => {'key'=>'value'}}, {'arrayref'=>['one', 'two']}, {'object'=> 'PAth::to:Obejct}]

Syntax
addMockWithReturnValueAndParameterCheck( $MethodName, $ReturnValue, $aParameterTypes );
Input
$MethodName (String)
name of method
$ReturnValue (integer String object refhash refarray)
value which will be returned by the mocked method
$aParameterTypes (refarray)
differnd parameter types

getMockObject

return the Mocked Module

Syntax
getMockObject( );
Return
$MockedModule (object)
MockedModule

mock

Based on the type and amout of parameters this is a shortcut for: addMock | mock('name', sub {}) addMockWithReturnValuemock | mock('name', 'returnValue') addMockWithReturnValueAndParameterCheck | mock('name', 'returnValue', [{'string'=>'jajaGenau'}])

Syntax
mock( $MethodName, $ReturnValue, $aParameterTypes );
Input
$MethodName (String)
name of method
$ReturnValue (integer String object refhash refarray)
value which will be returned by the mocked method
$aParameterTypes (refarray)
differnd parameter types

new

build the module which controlls the mocked module

Syntax
new( $FakeClassPath, $FakeParams );
Input
$FakeModulePath (String)
path to a existing module
$aFakeParams (refarray)
parameter list for this module
Return
$self (DE_EPAGES::Test::Mock::Mockify)
self