为什么我的脚本在访问未定义数组中的值时不触发通知?
我有一个如下所示的测试方法:
$row = $this->GetRowFromUserTable($id);
$this->assertLessThan(time(), time($row['last_update']));
当 $row 为 null 时,访问 $row['last_update'] 应触发 E_NOTICE 并且此测试应该失败,但事实并非如此。
这段代码在第一次断言时失败,所以我知道 $row 为空(fixture 是相同的):
$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
我读过这个问题: 如果代码抛出异常,我能否使 PHPUnit 失败注意? 但答案建议使用较新版本的 PHPUnit,但该答案是 2009 年的,所以不是它。
编辑:我使用 NetBeans IDE 6.9.1 来运行我的测试。
I have a test method which looks like this:
$row = $this->GetRowFromUserTable($id);
$this->assertLessThan(time(), time($row['last_update']));
When $row is null, access to $row['last_update'] should trigger an E_NOTICE and this test should fail, but it doesn't.
This code fails on first assert, so I know $row is null (fixture is the same):
$row = $this->GetRowFromUserTable($id);
$this->assertNotNull($row);
$this->assertLessThan(time(), time($row['last_update']));
When I write this:
$row = $this->GetRowFromUserTable($id);
$this->assertEquals(E_ALL, error_reporting());
$this->assertLessThan(time(), time($row['last_update']));
it succeeds, so I am also sure that error_reporting is right and this situation must have something to do with PHPUnit.
I use PHPUnit 3.5.6
I read this question:
Can I make PHPUnit fail if the code throws a notice?
but the answer suggests to use newer version of PHPUnit, but that answer is from 2009, so it's not it.
EDIT: I use NetBeans IDE 6.9.1 to run my test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据说“convertNoticesToExceptions”也是我唯一真正的猜测(+1)。
您可以在测试用例中的某个位置编写一个
$foo = $undefinedVariable;
来看看这是否也不会触发通知 // 不会使测试失败?要检查您的 phpunit / php 安装是否正常工作(也许可以缩小问题范围),您可以尝试:
并运行(注意 --no-configuration 部分只是为了确保您不会选择 phpunit.dist.xml 或phpunit.xml 意外)
它显示输出:
也许这有一点帮助
Saidly "convertNoticesToExceptions" was my only real guess too (+1).
Could you write a
$foo = $undefinedVariable;
somewhere in the testcase to see if that also will not not trigger a notice // not fail the test ?To check if your phpunit / php installation is working (and maybe narrow down the problem) you can try:
and run (note the --no-configuration part just to make sure you don't pick up a phpunit.dist.xml or phpunit.xml by accident)
It show output:
Maybe that helps a litte
您描述的具体情况没有给出
E_NOTICE
。访问已定义数组的未定义键会给出E_NOTICE
,但奇怪的是,尝试访问 null 值却不会,而数组则不会。The specific case you describe doesn't give an
E_NOTICE
. Accessing undefined keys of a defined array give anE_NOTICE
, but strangely trying to access null values as an array doesn't.您是否将 convertNoticesToExceptions 配置属性设置为“true”?
Do you have convertNoticesToExceptions configuration property set to "true"?