【发布时间】:2015-10-27 05:45:12
【问题描述】:
我是单元测试的新手,我正在试验 PHPUnit 框架。
我有一个调用另外两个函数的函数:
class Dummy{
public function dummyFunction(){
$this->anotherDummyFunction();
.....
$this->yetAnotherDummyFunction();
}
public function anotherDummyFunction(){
.....
}
public function yetAnotherDummyFunction(){
.....
}
}
我想测试调用 dummyFunction() 时是否调用了这两个函数。
这里是测试类:
class TestDummyClass {
public function testDummyFunction(){
$dummyClassMock = $this->getMockBuilder('Dummy')
->setMethods( array( 'anotherDummyFunction','yetAnotherDummyFunction' ) )
->getMock();
$dummyClassMock->expects($this->once())
->method( 'anotherDummyFunction' );
$dummyClassMock->expects($this->once())
->method( 'yetAnotherDummyFunction' );
$dummyClassMock->dummyFunction();
}
}
现在我注意到,如果我以这种方式模拟 Dummy 类,结果是
方法名称等于 anotherDummyFunction 的预期失败 当被调用 1 次时。方法预计会被调用 1 次, 实际调用了 0 次。
但是如果我以这种方式设置 Mock 对象
$dummyClassMock = $this->getMockBuilder('Dummy')
->setMethods( array( 'anotherDummyFunction' ) )
->getMock();
测试通过。最后,如果我用 setMethods(null) 设置 mock 对象,测试又失败了
似乎我可以传递一个只有一个元素的数组,我想要检查的方法是否被调用。但我在这里看到:https://jtreminio.com/2013/03/unit-testing-tutorial-part-5-mock-methods-and-overriding-constructors/ setMethods 用于注入传递的方法的返回值,所以这不会对调用本身产生影响,除非我将 dummyFunction 放在 setMethods 中(在这种情况下,函数将返回null而不调用其他两个方法,这样是的,测试必须失败)
我做错了什么?我已经看到在 setMethods() 中有多个方法的代码片段......为什么如果我将这些方法放在 setMethods 中,测试会失败?
谢谢
【问题讨论】:
-
嗯,这很奇怪,但它对我有用。顺便问一下,你确定
TestDummyClass是继承自PHPUnit_Framework_TestCase吗?你使用的是什么版本的phpunit?我的是4.6.9。 -
谢谢 Alexander,是的,它继承自
PHPUnit_Framework_TestCase,实际上是其他测试通过。我正在使用 text sublime 和用于 text sublime 的 PHPUnit 插件。 phpUnit 的版本是 4.6.6 但我不认为我可以使用新版本,因为插件。稍后我将尝试使用另一个带有新版本 phpUnit 的编辑器
标签: php unit-testing mocking phpunit