duanfeiqu1989 2017-03-30 07:03 采纳率: 100%
浏览 36
已采纳

当方法phpunit中没有参数时,assertFalse应该传递

My Test includes the wp_verify_nonce and implemented that in OOP environment. Now the problem is: If verifyNonce() has no arguemnts passed, then i want to expect return false due to the function:

verifyNonce():

     public function verifyNonce( $nonce ) {

          if( ! function_exists( 'wp_verify_nonce') || empty( $nonce ) || !is_string( $nonce ) )  return false; 

          return wp_verify_nonce( $nonce, $this->action );

     }

If $nonce is empty then it should return false since i passed this the if condition!

Now here i'm expecting that when there is no arguements passed, then it should return false:

testVerifyNonce():

public function testVerifyNonce() {

      $action        = 'my_action';
      $nonce         = '4832552f';

      $myWPNonce     = new test\MY_WP_Nonces( $action );

      \WP_Mock::userFunction( 'wp_verify_nonce', array( 

           'times'             =>   3,
           'return_in_order'   =>   array(false, 1, 2)

      ) );

      $this->assertFalse( $myWPNonce->verifyNonce() );

      $this->assertEquals( 1, $myWPNonce->verifyNonce( $nonce ) );

      $this->assertEquals( 2, $myWPNonce->verifyNonce( $nonce ) );

 }

Here's the output of my bash console:

PHPUnit 5.1.7 by Sebastian Bergmann and contributors.

..E....                                                             7 / 7 (100%)

Time: 206 ms, Memory: 8.00Mb

There was 1 error:

1) MYWPNonce_Test::testVerifyNonce
Missing argument 1 for Mywpnonces\src\wpnonce\MY_WP_Nonces::verifyNonce(), called in /mnt/c/Users/hm/Desktop/Github/Mywpnonces/src/tests/wpnonceTest.php on line 76 and defined

/mnt/c/Users/hm/Desktop/Github/Mywpnonces/src/wpnonce.php:37
/mnt/c/Users/hm/Desktop/Github/Mywpnonces/src/tests/wpnonceTest.php:76

FAILURES!
Tests: 7, Assertions: 6, Errors: 1.
  • 写回答

1条回答 默认 最新

  • doupu9251 2017-03-30 07:13
    关注

    PHPUnit tests your returning value of $myWPNonce->verifyNonce(). Before this can be accomplished you must pass the parameter, as it is defined in the method as mandatory parameter.

    PHPUnit does not test forgotten/missing parameters. PHP itself validates this.

    So pass something what should return false there as result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决