我正试图在Yii2测试中使用Codeception/AspectMock。 我有两节课:
一个:
namespace backend\baseModel;
class BaseModel
{
public static function getId()
{
return 9;
}
}
第二:
use \backend\baseModel\BaseModel;
class ModelA extends BaseModel
{
public static function getId()
{
return 5;
}
}
我正在尝试测试ModelA:
class TestModel extends \Codeception\Test\Unit
{
public function testGetId()
{
test::double(ModelA::getId(), ['getId' => 7]);
$this->assertSame(7, ModelA::getId());
}
}
我将进行错误测试:
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
我的配置文件:
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');
我尝试了此处提出的解决方案(第一个答案): Codeception/AspectMock Parent class not found by locator
错误不再出现,但是getId()方法返回5而不是7。
答案 0 :(得分:0)
您有静态方法,并且在ModelA中覆盖了getId函数,并且在测试中您正在调用ModelA类,因此在您遇到的情况下它应该返回5。