首页 文章

PHPUnit错误调用mock上的未定义方法

提问于
浏览
2

我收到错误:当我在Simple类mock上调用mymethod()时,调用未定义的方法Mock_SimpleInterface_8a93e777 :: mymethod() .

class PlaygroundTest extends \PHPUnit_Framework_TestCase 
{
    public function testMock()
    {
        $class     = $this->getMockBuilder('\Playground\Simple')->getMock();

        $class->mymethod();
    }
}

Simple类实现

namespace Playground;

class Simple
{

    public function mymethod()
    {
        print "Hey!";
    }
}

根据PHPUnit文档(https://phpunit.de/manual/5.1/en/test-doubles.html),它声明"By default, all methods of the original class are replaced with a dummy implementation that just returns null (without calling the original method)."

我不应该能够调用mymethod()并获得null返回值吗?我想避免指定所有类方法 . PHPUnit应该足够聪明,知道可以在mock上调用哪些方法 .

这是一个错误吗?我正在使用PHPUnit 5.1.4

1 回答

  • 3

    您的假设是正确的,因此您在其他地方有错误或未显示真实代码 .

    模拟类名 Mock_SimpleInterface_8a93e777 表示你实际上并不模拟 \Playground\Simple 而是 \Playground\SimpleInterface ,它可能不包含 mymethod()

相关问题