php - PHPUnit 中的单元测试 - 处理多个条件

标签 php unit-testing phpunit logical-operators

我想使用 PHPUnit 编写一个测试,其中包括检查以确保值是 string NULL

AFAIK,我可以像这样编写这样的测试:

if (is_string($value) || is_null($value)) { 
    $result = TRUE; 
} else { 
    $result = FALSE; 
} 

$this->assertTrue($result);

但是,我看到 PHPUnit 有一个 logicalOr() 方法,我不知道我是否应该使用它来进行更“原生”的测试?如果我应该使用它,我不知道该怎么做......

最佳答案

使用 phpunit v5.5 它(也)以这种方式工作:

if (is_string($value) || is_null($value)) { 
    $result = TRUE; 
} else { 
    $result = FALSE; 
} 

$this->assertThat($value, $this->logicalOr(
    $this->isType('string'),
    $this->isNull()
));

关于php - PHPUnit 中的单元测试 - 处理多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11721008/

相关文章:

PHP和mySQL登录安全疑虑

php - Laravel Controller 布局测试

phpunit - phpunit 测试完成后如何运行脚本

php - MySQL double /小数返回太多小数

javascript - 动态菜单和 Javascript 滚动

c# - 在这种情况下,您将如何应用单元测试?

java - Struts 1.1 和 Servlet 2.5 - 测试期间 JspException 的 NoClassDefFoundError

php - mock 似乎无法正常工作

unit-testing - Symfony2 单元测试中的模拟 Controller

php - Authorize.net: "createTransactionRequest"具有无效的子元素 "clientId"