Mocking SUT Himself

My question is about unit testing. Suppose we have the following class:

class X
{
    public function p1(){
       //logic
       $a = $this->p2();
       //more logic
    }

    public function p2(){
       //even more logic
    }
}

When writing the unit test method for p1, should I mock the p2 method?

I think that the test written for method p1 should only run and test method p1, not p2. But in order to understand that I have to make fun of class X and call the p1 method on this mock instance, as shown below.

$xMock = $this->getMockBuilder('\X')
    ->setMethods(array('p2'))
    ->getMock();

$xMock->expects($this->any())
    ->method('p2')
    ->will($this->returnValue($value));

$resultTobeAsserted = $xMock->p1();

, . , , SUT ( ). , SUT, , SUT, , , , . , SUT, , .

? ?

+5
3

unit test p1, mock p2-?

.

, , .

p2 .

, .

, .

?

, , - , , . .

( ), , , , .


:

The UNIT in unit testing.

, , .

PHP !

:

  • ( , db, $GLOBALS)

. .

+3

, SUT, , SUT, , , , .

, , , , .

+2

. p1, p2, , . , / p2. , / p2. , , , :

$xMock = $this->getMockBuilder('\X')
    ->setMethods(array('p2'))
    ->getMock();

$xMock->expects($this->any())
    ->method('p2')
    ->will($this->returnValue($value));

$resultTobeAsserted = $xMock->p1();
+1

All Articles