使用 Simpletest 进行单元测试的问题
我使用 PHP 和 simpletest 进行单元测试。我的测试工作正常,直到我尝试设置 cookie
try{
setcookie($name,$cookie,$cookie_expires );
}catch Exception($e){
blah
}
抛出异常,因为 simpletest
已经写出了标头信息,因此我得到以下信息:
意外的 PHP 错误 [无法修改标头信息 - 标头已由(输出从 /tests/simpletest/reporter.php:43 开始)发送] 严重性 [E_WARNING] 在 [blah_code.php 第 280 行]
我已经看到了有关捕获此问题的模糊解释与 $this->expectException(new Exception());
但没有进一步的文档或示例。有人可以提供一个可行的示例或向我指出文档吗?要明确的是。这不是我生成输出的代码,而是 SimpleTest。
I am using PHP and simpletest
for unit testing. My tests work fine until I try and set the cookie
try{
setcookie($name,$cookie,$cookie_expires );
}catch Exception($e){
blah
}
The exception is thrown because simpletest
has already written out header information so I get the following:
Unexpected PHP error [Cannot modify header information - headers already sent by (output started at /tests/simpletest/reporter.php:43)] severity [E_WARNING] in [blah_code.php line 280]
I've seen vague explanations on catching this with $this->expectException(new Exception());
but no further documentation or examples that work. Could someone provide a working example or point me to documentation? To be clear. This is NOT my code producing the output but rather SimpleTest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决这个问题的一种方法是使用输出缓冲。
您可以在 PHP 的配置中(也可能在 .htaccess 中)全局打开它,或者您可以使用 ob_start() 和其相关函数(
ob_get_clean()
、ob_end_flush()
等)。例如:然后:
或任何其他相关函数。如果您不这样做,我相信 PHP 会在文件末尾调用
ob_flush()
。One way to get around this is by using output buffering.
You can turn it on globally in PHP's configuration (and possibly in .htaccess), or you can use ob_start() and its related functions (
ob_get_clean()
,ob_end_flush()
, etc). For example:And then:
Or any of the other related functions. I believe PHP calls
ob_flush()
at the end of a file if you don't.当您之前有输出(header 函数)
setcookie 时,您会收到此错误($name,$cookie,$cookie_expires);
。确保之前没有任何
echos
或html
或text
或任何内容(连空格都没有)of
setcookie($name,$cookie,$cookie_expires );
。You get this error when you have output before (header functions)
setcookie($name,$cookie,$cookie_expires );
.Make sure you don't have any
echos
orhtml
ortext
or anything(NOT EVEN A SPACE) before<?php
ofsetcookie($name,$cookie,$cookie_expires );
.