ePages 6.17.17 - 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

Functions

GetParametersFromMockifyCall
addMock
addMockWithReturnValue
addMockWithReturnValueAndParameterCheck
getMockObject
new

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

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

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'] 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

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