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
- 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 |
|
Return |
|
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 |
|
new
build the module which controlls the mocked module
Syntax |
new( $FakeClassPath, $FakeParams ); |
Input |
|
Return |
|