2

我正在使用 SimpleTest 对我的 PHP 类进行单元测试。我添加了一个自定义基类,它spl_autoload_register可以自动加载我需要的类。

但是现在嘲弄成了一个问题。我如何模拟通过命名空间加载的类?

这基本上是我在我的测试课上所拥有的。

<?php
require_once('../../GGUnitTestCase.php');

Mock::generate('\Core\Routes\GGRoute');

class TestGGRouter extends GGUnitTestCase
{    
    function TestMethod()
    {
        $route = new \Core\Route\GGRoute(); // <-- This loads up fine!

        // But i need a Mock class, not the real one.
        // How to load a Mock in this case?
        $routeMock = new \Core\Routes\MockGGRoute(); // <-- Doesn't work!

        $this->assertTrue(false);
    }
}
?>

任何人都知道我如何仍然可以模拟通过命名空间加载的类(自动加载)?

4

1 回答 1

2

\命名空间类的名称中的(除前一个之外的全部)替换为::. 在你的例子中,这将是......

Mock::generate('Core::Routes::GGRoute');
于 2013-11-02T11:35:29.410 回答