我正在尝试测试一个类,每个方法都经过测试,除了最后一个方法,这对我来说很棘手,因为它在同一类中调用另一种方法并使用返回将字符串返回用户的值。
/**
* Get the total time elapsed as a
* human readable string
*
* @return string
*/
public function getElapsedTimeString()
{
$elapsed = $this->getElapsedTime();
return "{$elapsed} seconds elapsed.";
}
为了测试它,我需要确保 $this->getElapsedTime()
将返回像5或6之类的设置值,我一直在尝试使用模拟进行此操作,但是它不起作用,它每次都返回null
。P>
public function testGetElapsedTimeStringMethod()
{
// Create Mock of the CarbonTimer class
$mock = $this->getMockBuilder(CarbonTimer::class)
->getMock();
// Configure the Mock Method
$mock->method('getElapsedTime')
->willReturn(5);
$this->assertEquals("5 seconds elapsed.", $mock->getElapsedTimeString());
}
我在这里想念什么?抱歉,如果这是一个愚蠢的问题,我才刚刚开始使用phpunit,这有点不知所措
使它像这样起作用,简单地使用了我想覆盖的方法的名称的setMethods,尚不知道为什么它可以起作用。
public function testGetElapsedTimeStringMethod()
{
// Create Mock of the CarbonTimer class
$stub = $this->getMockBuilder(CarbonTimer::class)
->setMethods(['getElapsedTime'])
->getMock();
// Configure the Mock Method
$stub->method('getElapsedTime')
->willReturn(5);
$this->assertEquals("5 seconds elapsed.", $stub->getElapsedTimeString());
}