Sunday, 15 January 2012

php - After mocking object there are missing functions? -



php - After mocking object there are missing functions? -

admittedly new working mock objects have come across unusual behavior hope can explain me.

i have 2 classes class , class b. b extends , looks this.

abstract class { private $property public __constructor( $arg ) { //sets , constructor stuff } public function a() { //does stuff } public function b() { //does more stuff } public function c() { //calls b , stuff } } class b extends { const myconst = //some literal public __constructor() { parent::__constructor( self::myconst ); } }

since class abstract doing testing on class b. when testing function c need stub function b have done next in test.

$objectundertest = $this->getmockbuilder( 'b' ) ->setmethods( array( 'b' ) ) ->getmock(); $objectundertest->expects( $this->once() ) ->method( 'b' ) ->will( $this->returnvalue( 5 ) ); $result = $objectundertest->c(); //then assertions

the problem running when next test code executed phpunit complains can not find method c of $objectundertest. understand if had created mock object incorrectly , function c had become stub method opposed mock method still invokeable right? debugging confirm phpunit telling me outputted console results of get_class_methods() on mock object on normal instance of class b. outputted each.

//class b array( (int) 0 => '__construct', (int) 1 => 'a', (int) 2 => 'b', (int) 3 => 'c' ) //mocked b array( (int) 0 => '__clone', (int) 1 => 'b', (int) 2 => 'expects', (int) 3 => 'staticexpects', (int) 4 => '__phpunit_getinvocationmocker', (int) 5 => '__phpunit_getstaticinvocationmocker', (int) 6 => '__phpunit_hasmatchers', (int) 7 => '__phpunit_verify', (int) 8 => '__phpunit_cleanup' )

does know happened other methods class or explain behavior?

i've faced same problem while trying run code, included file classes (a , b) test class. helped me overcome problem.

php mocking phpunit

No comments:

Post a Comment