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 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。