当我在 Simple 类模拟上调用 mymethod() 时,出现错误:调用未定义的方法 Mock_SimpleInterface_8a93e777::mymethod() 。
class PlaygroundTest extends \PHPUnit_Framework_TestCase
{
public function testMock()
{
$class = $this->getMockBuilder('\Playground\Simple')->getMock();
$class->mymethod();
}
}
简单类实现
namespace Playground;
class Simple
{
public function mymethod()
{
print "Hey!";
}
}
根据 PHPUnit 文档 ( https://phpunit.de/manual/5.1/en/test-doubles.html ),它指出“默认情况下,原始类的所有方法都被替换为仅返回 null 的虚拟实现(不调用原始方法)。”
我不应该能够调用 mymethod() 并获得 null 返回值吗?我想避免指定所有类方法。 PHPUnit 应该足够聪明,知道哪些方法可以在模拟上调用,哪些方法不能调用。
这是一个错误吗?我正在使用 PHPUnit 5.1.4
最佳答案
您的假设是正确的,因此您在其他地方有错误或没有显示真实的代码。
模拟类名Mock_SimpleInterface_8a93e777
表明您实际上并不模拟\Playground\Simple
而是\Playground\SimpleInterface
,这可能不包含mymethod()
关于PHPUnit 错误调用模拟上的未定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34820260/