我在模拟方面配置中错过了什么?
问题描述:
我正在使用Symfony框架的guard component开发phpcas包。我的包正在工作,但我想做一些单元测试。我想测试我的CasAuthenticator。 PhpCAS library正在使用静态方法。所以我决定用Mock Aspect来嘲笑它。我在模拟方面配置中错过了什么?
我配置了看点,但我仍然有一个错误。
这是一个正在运行但失败的简化测试。
预计PhpCAS :: setDebug被调用,但它从未发生过。得到:
C:\ wamp64 \ WWW \ casguard \ casguard \厂商\ codeception \纵横模拟\ SRC \ AspectMock \代理\ Verifier.php:64
C:\ wamp64 \ WWW \ casguard \ casguard \测试\ SimpleTest.php:32
//root_dir/Tests/SimpleTest.php
namespace AlexandreT\Bundle\CasGuardBundle\Tests;
use PHPUnit\Framework\TestCase;
use AspectMock\Test as test;
use PhpCAS;
class SimpleTest extends TestCase
{
public function testAspectMock()
{
$phpCas = test::double('PhpCAS', ['setDebug' => function() {
echo 'YES I CALL THE MOCKED Debug function';
}]);
PhpCAS::setDebug();
$phpCas->verifyInvoked('setDebug', false);
}
protected function tearDown()
{
parent::tearDown();
test::clean();
}
}
输出不包含YES我CALL嘲笑调试功能,所以我认为PhpCAS不受看点嘲笑。
我仔细看了this documentation和我配置我的引导文件是这样的:
//root_dir/Tests/bootstrap.php
include __DIR__.'/../vendor/autoload.php'; // composer autoload
$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
'debug' => true,
'includePaths' => [
__DIR__.'/../vendor/jasig/phpcas',
],
]);
正如你可以看到,我增加了供应商目录,其中Cas.php
声明PhpCAS类。但它不会改变任何事情。我做了一些测试:bootstrap.php文件由phpunit加载。
我在模拟方面配置中错过了什么?
答
我添加了一行来配置缓存目录中我bootstrap.php
文件 ,我可以看到Cas.php
良好列入。
但我仍然有错误。当我探索缓存文件时,我发现phpcas库不遵守PSR0约定。 phpCAS类的第一个字母不是大写。
所以我编辑我的测试:
public function testAspectMock()
{
//$phpCas = test::double('PhpCAS', ['setDebug' => function() {
// ^
// |
// v
$phpCas = test::double('phpCAS', ['setDebug' => function() {
echo 'YES I CALL THE MOCKED Debug function';
}]);
phpCAS::setDebug(); //phpCAS instead of PhpCAS
$phpCas->verifyInvoked('setDebug', false);
//And I had an assertion else test is marked as risky.
self::expectOutputString('YES I CALL THE MOCKED Debug function');
}
2个字母... 2天的调试...... #Grrrr