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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)