【发布时间】:2017-03-22 21:39:44
【问题描述】:
我正在使用 Zend 框架开发单元测试的测试用例。使用作曲家安装 PHPUnit。我在 Windows 平台上工作。
我正在尝试模拟一种方法。在执行测试用例时,模拟方法没有被调用。相反,我尝试使用系统定义的方法,它按预期工作正常。
请看下面的代码:
/* Class CommonDataHandlerTest */
class CommonDataHandlerTest extends PHPUnit_Framework_TestCase{
public function mockTestCall(){
return 'foo';
}
}
class Apps_Sample_DataHandlerTest extends CommonDataHandlerTest{
public function setUp() {
....
}
public function tearDown() {
....
}
/*But here the method mockTestCall is not triggering while executing */
public function testReturnCallbackStub() {
$observer = $this->getMockBuilder('Apps_Sample_DataHandler')
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->getMock();
$that = $this;
$observer->method('getSampleData')
->will($this->returnCallback(
function() use($that) {
$that->mockTestCall();
}
));
$this->assertEquals('foo', $observer->getSampleData());
}
/*This is method is working as expected*/
public function testReturnCallbackStubSystem() {
$observer = $this->getMockBuilder('Apps_Sample_DataHandler')
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->getMock();
$observer->method('getSampleData')
->will($this->returnCallback('str_rot13'));
$this->assertEquals('foo', $observer->getSampleData('ssb'));
}
}
在执行上述代码时,测试用例方法“testReturnCallbackStubSystem”按预期工作。
但是测试用例方法“testReturnCallbackStub”不起作用。这里没有触发模拟方法“mockTestCall”。
请问我可以知道原因吗? 有人请帮我解决这个问题。如果您想了解更多详细信息,请告诉我。
提前谢谢...
【问题讨论】:
标签: php unit-testing