PHPUnit错误致命错误:调用未定义方法Mock_Game_073a8e20::method()


PHPUnit Error Fatal error: Call to undefined method Mock_Game_073a8e20::method()

我目前正在观看一个使用PHP Unit的指南,不知怎么的,当涉及到mock时,我总是得到这个错误。

游戏类

class Game {
    protected $title;   protected $imagePath;   protected $ratings;
    public function getAverageScore(){
        $ratings = $this->getRatings();         $numRatings = count($ratings);
        $total = 0;
        if($numRatings == 0){           return null;        }

        foreach($ratings as $rating){
            $total = $rating->getScore();
        }
        return $total / $numRatings;
    }
    public function isRecommended()
    {
        return $this->getAverageScore() >= 3;
    }
    public function getTitle(){         return $this->title;    }
    public function setTitle($value){       $this->title = $value;  }
    public function getImagePath(){         if($this->imagePath == null){           return '/images/placeholder.jpg';       }       return $this->imagePath;    }
    public function setImagePath($value){       return $this->imagePath = $value;   }
    public function getRatings(){       return $this->ratings;  }
    public function setRatings($value){         return $this->ratings = $value;     }
}
测试用例

public function testAverageScore_With6And8_Returns7(){
    $ratings1 = $this->getMock('Rating', ['getScore']);
    $ratings1->method('getScore')
             ->willReturn(6);
    $ratings2 = $this->getMock('Rating', ['getScore']);
    $ratings2->method('getScore')
             ->willReturn(8);
    $game = $this->getMock('Game', ['getRatings']);
    $game->method('getRatings')
         ->willReturn([$ratings1, $ratings2]);
    $this->assertEquals(7, $game->getAverageScore());
}
错误:

E:'xampp'htdocs'gamebook>phpunit src/Test/Unit/GameTest.php3.7.21 by Sebastian Bergmann.

…致命错误:调用未定义的方法Mock_Rating_5c2598e3::方法()E:'xampp'htdocs'gamebook'src'Test'Unit'GameTest.php第40行

调用堆栈:0.0670 126024{主要}()E: ' xampp ' php ' phpunit): 00.1800 361592PHPUnit_TextUI_Command: main () E: ' xampp ' php ' phpunit): 460.1800 365008PHPUnit_TextUI_Command -> run () E: ' xampp ' php '梨' PHPUnit) ' TextUI ' Command.php: 1290.3070 1401944PHPUnit_TextUI_TestRunner -> doRun () E: ' xampp ' php '梨' PHPUnit) ' TextUI ' Command.php: 1760.3200 1614568PHPUnit_Framework_TestSuite -> run () E: ' xampp ' php '梨' PHPUnit) ' TextUI ' TestRunner.php: 3490.3810 1873016PHPUnit_Framework_TestSuite ->小牛()E: ' xampp ' php '梨' PHPUnit) ' Framework ' TestSuite.php: 7450.3810 1873016PHPUnit_Framework_TestCase -> run () E: ' xampp ' php '梨' PHPUnit) ' Framework ' TestSuite.php: 7750.3810 1872984PHPUnit_Framework_TestResult -> run () E: ' xampp ' php '梨' PHPUnit) ' Framework ' TestCase.php: 7760.3820 1873600PHPUnit_Framework_TestCase -> runBare () E: ' xampp ' php '梨' PHPUnit) ' Framework ' TestResult.php: 6480.3830 1904096PHPUnit_Framework_TestCase ->小牛()E: ' xampp ' php '梨' PHPUnit) ' Framework ' TestCase.php: 8310.3830 1904592ReflectionMethod -> invokeArgs () E: ' xampp ' php '梨' PHPUnit) ' Framework ' TestCase.php: 9760.3830 1904704配子-> testAverageScore_With6And8_Returns7 ()E: ' xampp ' php '梨' PHPUnit) ' Framework ' TestCase.php: 976

getMock函数在PHPUnit 5.4中已被弃用:

PHPUnit'Framework'TestCase::getMock()方法已弃用。请使用PHPUnit'Framework'TestCase::createMock()或PHPUnit) ' Framework ' TestCase: getMockBuilder()。

您的代码中没有包含Rating类,但是如果包含了,您可以这样模拟它:

$ratings1 = $this->createMock('Rating');
$ratings1->method('getScore')
    ->willReturn(6);

同样,在最后一个mock语句中,您传入了两个参数,但是函数:

public function getRatings() {
    return $this->ratings;
}

没有两个参数,它必须是:

public function getRatings($rating1, $rating2) {      
    return ($rating1->getScore() + $rating2->getScore()) / 2;
}

然后你不模拟那个调用,你用模拟的评级对象来调用它:

$game = new Game();
$answer = $game->getRatings($ratings1, $ratings2);
$this->assertSame(7, $answer);

我想你的意思是让getRatings获取一个数组的评级,但我把它留给你去编码…

要像原始代码那样创建部分模拟对象,可以使用:

'PHPUnit'Framework'TestCase::createPartialMock(string className, array methods)

所以新代码看起来像:

$ratings1 = $this->createPartialMock('Rating', ['getScore']);

只有名称在数组中的方法才会被替换为可配置的test double。其他方法的行为不改变