【问题标题】:phpunit assert contains two possibilitiesphpunit assert 包含两种可能
【发布时间】:2018-03-26 13:59:48
【问题描述】:

我如何要求 PHPUnit 断言一个长字符串包含两个可能的选项之一?任何一个结果都应该断言为真。像这样?

$multi_kilobyte_string = "lorem ipsum...";
$option1 = "dolor";
$option2 = "amet";
$this->assertContains([$option1, $option2], $multi_kilobyte_string);

【问题讨论】:

    标签: phpunit


    【解决方案1】:

    这个问题的解决方案很简单(但没有完全记录):

    public function testContainsThisOrThat()
    {
        $multi_kilobyte_string = "lorem ipsum...";
        $option1 = "dolor";
        $option2 = "amet";
    
        $this->assertThat($multi_kilobyte_string,
            $this->logicalOr(
                $this->stringContains($option1),
                $this->stringContains($option2)
            )
        );
    }
    

    PHPUnit 的最佳信息来源是它的source code。此外,您的编辑器的自动完成功能(假设您使用的是提供它的功能)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 2014-03-27
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多