PHPUnit错误致命错误:调用未定义的方法Mock_Game_073a8e20 :: method()
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHPUnit错误致命错误:调用未定义的方法Mock_Game_073a8e20 :: method(),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3718字,纯文字阅读大概需要6分钟。
内容图文
![PHPUnit错误致命错误:调用未定义的方法Mock_Game_073a8e20 :: method()](/upload/InfoBanner/zyjiaocheng/684/ad8ad93ca28f4799b3506eb83da2e85e.jpg)
我目前正在观看使用PHP Unit的指南,当涉及到模拟时,我总是会收到此错误.
游戏类
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.php PHPUnit
3.7.21 by Sebastian Bergmann.… Fatal error: Call to undefined method
Mock_Rating_5c2598e3::method() in
E:\xampp\htdocs\gamebook\src\Test\Unit\GameTest.php on line 40Call Stack:
0.0670 126024 1. {main}() E:\xampp\php\phpunit:0
0.1800 361592 2. PHPUnit_TextUI_Command::main() E:\xampp\php\phpunit:46
0.1800 365008 3. PHPUnit_TextUI_Command->run() E:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
0.3070 1401944 4. PHPUnit_TextUI_TestRunner->doRun() E:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
0.3200 1614568 5. PHPUnit_Framework_TestSuite->run() E:\xampp\php\pear\PHPUnit\TextUI\TestRunner.php:349
0.3810 1873016 6. PHPUnit_Framework_TestSuite->runTest() E:\xampp\php\pear\PHPUnit\Framework\TestSuite.php:745
0.3810 1873016 7. PHPUnit_Framework_TestCase->run() E:\xampp\php\pear\PHPUnit\Framework\TestSuite.php:775
0.3810 1872984 8. PHPUnit_Framework_TestResult->run() E:\xampp\php\pear\PHPUnit\Framework\TestCase.php:776
0.3820 1873600 9. PHPUnit_Framework_TestCase->runBare() E:\xampp\php\pear\PHPUnit\Framework\TestResult.php:648
0.3830 1904096 10. PHPUnit_Framework_TestCase->runTest() E:\xampp\php\pear\PHPUnit\Framework\TestCase.php:831
0.3830 1904592 11. ReflectionMethod->invokeArgs() E:\xampp\php\pear\PHPUnit\Framework\TestCase.php:976
0.3830 1904704 12. GameTest->testAverageScore_With6And8_Returns7()
E:\xampp\php\pear\PHPUnit\Framework\TestCase.php:976
解决方法:
从PHPUnit 5.4起,不赞成使用getMock函数:
The PHPUnit\Framework\TestCase::getMock() method has been deprecated.
Please use PHPUnit\Framework\TestCase::createMock() or
PHPUnit\Framework\TestCase::getMockBuilder() instead.
Rating类不包含在您的代码中,但如果包含,则可以这样模拟:
$ratings1 = $this->createMock('Rating');
$ratings1->method('getScore')
->willReturn(6);
另外,在最后一个模拟语句中,您传入了两个参数,但函数:
public function getRatings(){ return $this->ratings; }
没有两个参数,它必须是:
public function getRatings($rating1, $rating2) {
return ($rating1->getScore() + $rating2->getScore())/2;
}
然后,您不模拟该调用,而是使用模拟的Rating对象调用它:
$game = new Game();
$answer = $game->getRatings($ratings1, $ratings2);
$this->assertSame(7,$answer);
我认为您的意思是让getRatings接受一系列的Ratings,但我留给您编写代码…
内容总结
以上是互联网集市为您收集整理的PHPUnit错误致命错误:调用未定义的方法Mock_Game_073a8e20 :: method()全部内容,希望文章能够帮你解决PHPUnit错误致命错误:调用未定义的方法Mock_Game_073a8e20 :: method()所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。