admin管理员组

文章数量:1426855

This question is related to this SO post

I'm testing a plugin, and in it I have

public function enqueue_scripts() {
    $main_style = 'public/scripts/application.js';

    wp_register_script( 'plugin-scripts', plugin_dir_url( __DIR__ ) . $main_script, array() );

    wp_enqueue_script( 'plugin-scripts' );
}

The plugin is using composer and npm, so it needs to be built to work.

And it is built (locally where I'm testing it, it works in a local instance I have on VVV). The test I wrote looks like this

public function test_enqueued_scripts() {
    $this->admin->enqueue_scripts();
    $this->assertTrue( wp_script_is( 'plugin-scripts' ) );
}

$this->admin is just an instance of the class where my enqueue method is in the setUp() method.

The test fails (Failed asserting that false is true), and I wanted to see what's wrong so I added

public function enqueue_scripts() {
    $main_style = 'public/scripts/application.js';
    error_log( print_r( plugin_dir_url( __DIR__ ), true ) );
    error_log( print_r( __DIR__, true ) );
    wp_register_script( 'plugin-scripts', plugin_dir_url( __DIR__ ) . $main_script, array() );

    wp_enqueue_script( 'plugin-scripts' );
}

And ran the unit test, I saw

/

For the plugin_dir_url( __DIR__ ), and for the __DIR__ I have

/vagrant-local/www/projects/project/public_html/wp-content/plugins/my-plugin/

The test WordPress instance is located in a temporary folder that looks something like

/var/folders/xj/14b3w8w51ll6rw09q72p45cr0000gn/T/wordpress/

But my plugins are not installed there. I'm guessing the tests are only using that as a source for the core functionality. Also the bootstrap.php file is referencing the wordpress-develop that is located in my VVV folder.

What I don't understand is why do I get that example url? And how can I successfully test the enqueueing/registering of the script?

This question is related to this SO post

I'm testing a plugin, and in it I have

public function enqueue_scripts() {
    $main_style = 'public/scripts/application.js';

    wp_register_script( 'plugin-scripts', plugin_dir_url( __DIR__ ) . $main_script, array() );

    wp_enqueue_script( 'plugin-scripts' );
}

The plugin is using composer and npm, so it needs to be built to work.

And it is built (locally where I'm testing it, it works in a local instance I have on VVV). The test I wrote looks like this

public function test_enqueued_scripts() {
    $this->admin->enqueue_scripts();
    $this->assertTrue( wp_script_is( 'plugin-scripts' ) );
}

$this->admin is just an instance of the class where my enqueue method is in the setUp() method.

The test fails (Failed asserting that false is true), and I wanted to see what's wrong so I added

public function enqueue_scripts() {
    $main_style = 'public/scripts/application.js';
    error_log( print_r( plugin_dir_url( __DIR__ ), true ) );
    error_log( print_r( __DIR__, true ) );
    wp_register_script( 'plugin-scripts', plugin_dir_url( __DIR__ ) . $main_script, array() );

    wp_enqueue_script( 'plugin-scripts' );
}

And ran the unit test, I saw

http://example/wp-content/plugins/Users/dingo_d/vagrant-local/www/projects/project/public_html/wp-content/plugins/my-plugin/

For the plugin_dir_url( __DIR__ ), and for the __DIR__ I have

/vagrant-local/www/projects/project/public_html/wp-content/plugins/my-plugin/

The test WordPress instance is located in a temporary folder that looks something like

/var/folders/xj/14b3w8w51ll6rw09q72p45cr0000gn/T/wordpress/

But my plugins are not installed there. I'm guessing the tests are only using that as a source for the core functionality. Also the bootstrap.php file is referencing the wordpress-develop that is located in my VVV folder.

What I don't understand is why do I get that example url? And how can I successfully test the enqueueing/registering of the script?

Share Improve this question asked Jun 7, 2018 at 8:11 dingo_ddingo_d 1,9602 gold badges25 silver badges49 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You need to run do_action('wp_enqueue_scripts') prior to executing your assertions. This article has more details and an example.

本文标签: oopIntegration tests test script enqueueregister fails