加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

phpunit:为什么这个assertContains传递?

发布时间:2020-12-13 16:57:36 所属栏目:PHP教程 来源:网络整理
导读:我的测试应该失败,但它通过了: public function test_getValidProviderCodes(){ $aTest = PRIDEReflection::executeStaticMethodForClassName(Apps_DoctorsReports::class,"getValidProviderCodes"); print_r($aTest); $this-assertContains("xxxxxxxxxxxx
我的测试应该失败,但它通过了:

public function test_getValidProviderCodes(){
    $aTest = PRIDEReflection::executeStaticMethodForClassName(Apps_DoctorsReports::class,"getValidProviderCodes");
    print_r($aTest);
    $this->assertContains("xxxxxxxxxxxxxx",$aTest);
}

输出:

Testing started at 8:53 AM ...
PHPUnit 4.6.6 by Sebastian Bergmann and contributors.

Configuration read from C:inetpubIntranet_Localphpunitphpunit.xml

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => MAYER
    [4] => MAY00
    [5] => MAYERIC
    [6] => COH00
    [7] => COH01
    [8] => POWELL
    [9] => POW00
    [10] => JOHN00
    [11] => FINO
    [12] => POL01
    [13] => NONAP
    [14] => RAYE00
    [15] => HOPS00
    [16] => CHAH00
)
 - 


Time: 1.24 seconds,Memory: 8.25Mb

OK (1 test,1 assertion)

值“xxxxxxxxxxxxxx”显然不在该数组中.我已经使用了这个函数数百次,从未见过这种行为.

(如果我将$aTest更改为[],则测试失败.)

这是另一个测试运行:

public function test_getValidProviderCodes(){
    $aTest = PRIDEReflection::executeStaticMethodForClassName(Apps_DoctorsReports::class,"getValidProviderCodes");
    $this->assertContains("S01",implode(",",$aTest));
}

输出:

Testing started at 9:04 AM ...
PHPUnit 4.6.6 by Sebastian Bergmann and contributors.

Configuration read from C:inetpubIntranet_Localphpunitphpunit.xml


Failed asserting that '0,1,2,M01,M03,M04,M05,N02,C01,C02,C03,C04,P01,P02,P03,P04,P05,P06,P07,R01,H01,J01,J02' contains "S01".
 C:inetpubIntranet_LocalphpunitlibraryclassesappsDoctorsReportsTest.php:61



Time: 1.54 seconds,Memory: 8.75Mb


FAILURES!
Tests: 1,Assertions: 1,Failures: 1.

解决方法

assertContains也可以检查对象标识(默认行为).因此,为了跳过这个,您需要将可选标志传递给该方法.这个问题已经涵盖了 here on github.

因此,为了使测试失败,只需将最后一个可选参数checkForNonObjectIdentity传递给值true,如下所示:

public function test_getValidProviderCodes(){

    $aTest =
        array(
            0,'MAYER'
        );
    print_r($aTest);
    $this->assertContains(
        "xxxxxxxxxxxxxx",$aTest,'message',false,true,true);

}

与输出:

Failed asserting that an array contains ‘xxxxxxxxxxxxxx’.

希望这有帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读