1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

assertWPErrorでPHPUnitでのユニットテスト中にWP_Errorをテストする

Last updated at Posted at 2017-01-10

WP_UnitTestCaseを利用している場合、WP_Errorオブジェクトを正しく返して居るか否かのテストにassertWPErrorが利用できます。

        function test_is_wp_error() {
                $test = new WP_Error( 'something happen' );
                $this->assertWPError( $test );
        }

ちなみにassertNotWPErrorを使うと、「WP_Errorではないこと」をテストできます。

	function test_is_not_wp_error() {
		$test = 'nothing happen';
		$this->assertNotWPError( $test );
	}

こちらはWordPress4.5.4だとPHP Fatal error: Call to undefined methodが出ましたので、利用には要注意です。

おまけ:両関数のソースコード

ソースコード:http://develop.svn.wordpress.org/trunk/tests/phpunit/includes/testcase.php

	function assertWPError( $actual, $message = '' ) {
		$this->assertInstanceOf( 'WP_Error', $actual, $message );
	}

	function assertNotWPError( $actual, $message = '' ) {
		if ( is_wp_error( $actual ) && '' === $message ) {
			$message = $actual->get_error_message();
		}
		$this->assertNotInstanceOf( 'WP_Error', $actual, $message );
	}

PHPUnitのassertInstanceOf()と、assertNotInstanceOf()のラッパーになっています。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?