php - 为什么我的脚本在访问未定义数组中的值时不触发通知?

标签 php phpunit

我有一个如下所示的测试方法:

$row = $this->GetRowFromUserTable($id);
$this->assertLessThan(time(), time($row['last_update']));

当 $row 为 null 时,对 $row['last_update'] 的访问应该触发 E_NOTICE 并且此测试应该失败,但事实并非如此。

此代码在第一个断言时失败,所以我知道 $row 为空(夹具相同):

$row = $this->GetRowFromUserTable($id);
$this->assertNotNull($row);
$this->assertLessThan(time(), time($row['last_update']));

当我写这篇文章时:

$row = $this->GetRowFromUserTable($id);
$this->assertEquals(E_ALL, error_reporting());
$this->assertLessThan(time(), time($row['last_update']));

成功了,所以我也确定error_reporting是对的,这种情况肯定和PHPUnit有关。

我使用 PHPUnit 3.5.6

我读了这个问题: Can I make PHPUnit fail if the code throws a notice? 但答案建议使用较新版本的 PHPUnit,但该答案来自 2009 年,所以不是。

编辑:我使用 NetBeans IDE 6.9.1 来运行我的测试。

最佳答案

据说“convertNoticesToExceptions”也是我唯一真实的猜测 (+1)。

你能不能在测试用例的某处写一个 $foo = $undefinedVariable; 看看它是否也不会触发通知//不会失败测试?

要检查您的 phpunit/php 安装是否正常工作(并可能缩小问题范围),您可以尝试:

<?php

class myTest extends PHPUnit_Framework_TestCase {
    public function testFoo() {
        $x = $foo;
        $this->assertTrue(true);
    }
}

然后运行(注意 --no-configuration 部分只是为了确保您不会意外地选择 phpunit.dist.xml 或 phpunit.xml)

phpunit --no-configuration foo.php 

它显示输出:

PHPUnit 3.5.6 by Sebastian Bergmann.

E

Time: 0 seconds, Memory: 2.50Mb

There was 1 error:

1) myTest::testFoo Undefined variable: foo

/home/user/foo.php:5

也许这有点帮助

关于php - 为什么我的脚本在访问未定义数组中的值时不触发通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4614630/

相关文章:

php - Nette Framework - 获取当前 session ID

php - 获取php中给定url的所有href属性列表

Phpunit 测试给出警告,没有在类里面找到测试

php - 如何在 Symfony 4 和 Simple-PHPUnit 中调试测试?

php - PHPUnit 模拟对象是否可以替换类中创建的对象?

php - Supervisord 让我的 Laravel 队列 :listen throw InvalidArgumentException

php - 使用准备好的语句上传图像文件位置

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

php - mock 似乎无法正常工作

php - 登录后连接到不同的数据库?