我想对一个类执行单元测试,我的目标是:我想使用函数检查插件是否被激活:is_plugin_active
class WC_Custom_Variable_Products_Dependencies {
public function __construct() {
add_action( \'admin_init\', [$this, \'check_environment\']);
}
public function check_environment(){
return is_plugin_active(
\'woocommerce-custom-variable-products/woocommerce-custom-variable-products.php\'
);
}
}
de类测试:
require_once \'class-wc-custom-variable-products-dependencies.php\';
class WC_Custom_Variable_Products_DependenciesTest extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
$this->class_instance = new WC_Custom_Variable_Products_Dependencies();
}
public function test_check_environment(){
$result = $this->class_instance->check_environment();
$this->assertTrue($result);
}
断言返回的总是False。
我的插件被激活,功能is_plugin_active
如果从浏览器执行,则返回True:
add_action(\'admin_init\', function(){
var_dump(is_plugin_active(
\'woocommerce-custom-variable-products/woocommerce-custom-variable-products.php\'
));
});
我认为
admin_init
测试中未执行挂钩。这是真的还是假的?
最合适的回答,由SO网友:Younes.D 整理而成
我找到了原因。解决方案如下:您必须在tests / bootstrap.php
文件:
$GLOBALS[ \'wp_tests_options\' ] = array(
\'active_plugins\' => array(
\'YOUR-PLUGIN/YOUR-PLUGIN.php\'
)
)