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 |
Functions
- GetCallCount
- GetParametersFromMockifyCall
- WasCalled
- addMethodSpy
- addMethodSpyWithParameterCheck
- addMock
- 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 |
|
Return |
|
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 |
|
Return |
|
WasCalled
returns true if the Method was called
Syntax |
WasCalled( $MockifiedMockedObject, $MethodName ); |
Input |
|
Return |
|
addMethodSpy
Give option to observe a Method while keeping the original functionality.
Syntax |
addMethodSpy( $MethodName ); |
Input |
|
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 |
|
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 |
|
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 |
|
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 |
|
getMockObject
return the Mocked Module
Syntax |
getMockObject( ); |
Return |
|
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 |
|
new
build the module which controlls the mocked module
Syntax |
new( $FakeClassPath, $FakeParams ); |
Input |
|
Return |
|