dreamfly0514 2018-11-13 08:11
浏览 70

Codeception / AspectMock定位器i Yii2找不到父类

I'm trying to use Codeception/AspectMock in Yii2 tests. I have two classes:

One:

namespace backend\baseModel;
class BaseModel
{
    public static function getId()
    {
        return 9;
    }
}

Second:

use \backend\baseModel\BaseModel;

class ModelA extends BaseModel
{
    public static function getId()
    {
        return 5;
    }
}

I am trying to test ModelA:

class TestModel extends \Codeception\Test\Unit
{

    public function testGetId()
    {
        test::double(ModelA::getId(), ['getId' => 7]);
        $this->assertSame(7, ModelA::getId());
    }
}

I will do the test with an error:

1) TestMode: Get id
 Test  tests/unit/TestModelTest.php:testGetId

  [InvalidArgumentException] Class backend\baseModel\BaseModel was not found by locator

#1  /var/www/vendor/goaop/parser-reflection/src/ReflectionEngine.php:125
#2  /var/www/vendor/goaop/parser-reflection/src/ReflectionEngine.php:140
#3  /var/www/vendor/goaop/parser-reflection/src/ReflectionClass.php:44
#4  /var/www/vendor/goaop/parser-reflection/src/ReflectionClass.php:150
#5  /var/www/vendor/goaop/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:446
#6  /var/www/vendor/goaop/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:924
#7  /var/www/vendor/goaop/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:317
#8  /var/www/vendor/goaop/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:303
#9  /var/www/vendor/goaop/framework/src/Instrument/Transformer/CachingTransformer.php:121
#10 /var/www/vendor/goaop/framework/src/Instrument/Transformer/CachingTransformer.php:78

My configuration file:

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
defined('YII_APP_BASE_PATH') or define('YII_APP_BASE_PATH', __DIR__.'/../../');

require_once(YII_APP_BASE_PATH . '/vendor/autoload.php');
$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
    'debug' => true,
    'cacheDir'     => __DIR__ . '/_data/cache',
    'includePaths' => [__DIR__.'/../../backend'],
]);
$kernel->loadFile(YII_APP_BASE_PATH . '/vendor/yiisoft/yii2/Yii.php');

I tried the solution proposed here (in the first answer): Codeception/AspectMock Parent class not found by locator

The error stops appearing, but then the getId () method returns 5 instead of 7.

  • 写回答

1条回答 默认 最新

  • dongren4147 2018-11-13 09:45
    关注

    You have static method and you override getId function in ModelA, and in test you are invoking ModelA class, so it should return 5 in you case.

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助