【问题标题】:Distinguish between NULL and FALSE with PHPUnit用 PHPUnit 区分 NULL 和 FALSE
【发布时间】:2011-08-23 18:51:49
【问题描述】:

有谁知道用 PHPUnit 区分 FALSE 和 NULL 的可靠方法吗?

我试图在我的断言中区分返回值中的 NULL 和 FALSE。

这失败了:

$this->assertNotEquals(FALSE, NULL);

这些断言通过了:

$this->assertFalse(NULL);
$this->assertNull(FALSE);

编辑:在某些情况下,这是为了区分错误状态 (FALSE) 和空结果 (NULL)。为了确保函数正确返回,我需要区分两者。 谢谢

编辑... 根据我正在测试的一些问题,我正在添加测试。

Class testNullFalse extends PHPUnit_Framework_TestCase{


    public function test_null_not_false (){
      $this->assertNotEquals(FALSE, NULL, "False and null are not the same");
    }

    public function test_null_is_false (){
      $this->assertFalse(NULL, "Null is clearly not FALSE");
    }

    public function test_false_is_null (){
      $this->assertNull(FALSE, "False is clearly not NULL");
    }

    public function test_false_equals_null(){
      $this->assertEquals(FALSE, NULL, "False and null are not equal");
    }

    public function test_false_sameas_null(){
      $this->assertSame(FALSE, NULL, "False and null are not the same");
    }

    public function test_false_not_sameas_null(){
      $this->assertNotSame(FALSE, NULL, "False and null are not the same");
    }
}

还有结果。

PHPUnit 3.5.10 by Sebastian Bergmann.

FFF.F.

Time: 0 seconds, Memory: 5.50Mb

There were 4 failures:

1) testNullFalse::test_null_not_false
False and null are not the same
Failed asserting that <null> is not equal to <boolean:false>.

2) testNullFalse::test_null_is_false
Null is clearly not FALSE
Failed asserting that <null> is false.

3) testNullFalse::test_false_is_null
False is clearly not NULL
Failed asserting that <boolean:false> is null.

4) testNullFalse::test_false_sameas_null
False and null are not the same
<null> does not match expected type "boolean".

FAILURES!
Tests: 6, Assertions: 6, Failures: 4.

【问题讨论】:

  • PHPunit(至少 v3.7+,如果不是之前)现在有 assertSame() 来检查两个变量是否具有相同的类型和值。

标签: php unit-testing null phpunit


【解决方案1】:

这些断言使用==,它将执行类型强制。 Hamcrest 有identicalTo($value),它使用===,我相信PHPUnit 有assertSame($expected, $actual),它也是如此。

self::assertSame(false, $dao->getUser(-2));

更新:回答您的评论,“它可以是 NULL 或对象”:

$user = $dao->getUser(-2);
self::assertTrue($user === null || is_object($user));

使用 Hamcrest 断言更有表现力,尤其是在失败的情况下:

assertThat($dao->getUser(-2), anyOf(objectValue(), nullValue()));

【讨论】:

  • 谢谢。也许你可以帮助我解决一个哲学问题以及处理这个问题...... assertFalse,assertTrue,assertNull 等如果它们确实键入强制,那么它们有什么意义?
  • @KyleWpppd 我仍然认为它们是类型安全的,如果 $this->assertFalse(null);不会导致测试失败:)
  • 你是对的。困扰我的是如何断言我的结果不是错误的问题。它可以是NULL 或一个对象。但是FALSE 表示查询有问题。
  • @David Harkness - 谢谢。我想知道是否存在风格问题,但我想让它发挥作用比让它漂亮更重要。 =)
  • @KyleWpppd - 使用assertTrue()assertFalse() 的主要问题是失败消息毫无价值:“预期为真,但为假”。您可以随时传递描述,例如self::assertTrue('user or null', ...);
【解决方案2】:

自己执行比较,使用严格类型操作符。

$this->assertTrue(false !== null);

http://php.net/operators.comparison

【讨论】:

    【解决方案3】:

    @David 使用 assertSame (+1) 是对的,它会为您进行 === 严格比较。


    但是让我问你:

    您使用的是哪个版本的 phpunit?

    这个断言:

    $this->assertFalse(null);
    

    应该产生错误!

    示例代码:

    <?php
    
    class mepTest extends PHPUnit_Framework_TestCase {
    
        public function testFalseNull() {
            $this->assertFalse(null);
        }
    
        public function testNullFalse() {
            $this->assertNull(false);
       }
    }
    

    结果:

    phpunit mepTest.php
    
    PHPUnit 3.5.12 by Sebastian Bergmann.
    
    FF
    
    Time: 0 seconds, Memory: 3.00Mb
    
    There were 2 failures:
    
    1) mepTest::testFalseNull
    Failed asserting that <null> is false.
    
    /home/.../mepTest.php:6
    
    2) mepTest::testNullFalse
    Failed asserting that <boolean:false> is null.
    
    /home/.../mepTest.php:10
    
    FAILURES!
    Tests: 2, Assertions: 2, Failures: 2.
    

    【讨论】:

    • 我得看一个小时左右。我认为是 3.5,但我会确认。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2016-03-27
    相关资源
    最近更新 更多