How can I get PHPUnit MockObjects to return different values based on a parameter?

前端 未结 11 1610
忘掉有多难
忘掉有多难 2020-12-04 05:43

I\'ve got a PHPUnit mock object that returns \'return value\' no matter what its arguments:

// From inside a test...
$mock = $this->getMock(\         


        
11条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 06:19

    $this->BusinessMock = $this->createMock('AppBundle\Entity\Business');
    
        public function testBusiness()
        {
            /*
                onConcecutiveCalls : Whether you want that the Stub returns differents values when it will be called .
            */
            $this->BusinessMock ->method('getEmployees')
                                    ->will($this->onConsecutiveCalls(
                                                $this->returnArgument(0),
                                                $this->returnValue('employee')                                      
                                                )
                                          );
            // first call
    
            $this->assertInstanceOf( //$this->returnArgument(0),
                    'argument',
                    $this->BusinessMock->getEmployees()
                    );
           // second call
    
    
            $this->assertEquals('employee',$this->BusinessMock->getEmployees()) 
          //$this->returnValue('employee'),
    
    
        }
    

提交回复
热议问题