使用 Simpletest 进行单元测试的问题

发布于 2024-10-23 11:53:35 字数 533 浏览 1 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的故事 2024-10-30 11:53:35

解决这个问题的一种方法是使用输出缓冲。

您可以在 PHP 的配置中(也可能在 .htaccess 中)全局打开它,或者您可以使用 ob_start() 和其相关函数(ob_get_clean()ob_end_flush() 等)。例如:

ob_start();
// your SimpleTest here.
// your header/ cookie manipulation here.

然后:

ob_end_clean(); // Stop buffering and dump everything (don't echo).
ob_end_flush(); // Stop buffering and echo out the buffer.
ob_get_clean(); // Stop buffering and return everything as a string.

或任何其他相关函数。如果您不这样做,我相信 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:

ob_start();
// your SimpleTest here.
// your header/ cookie manipulation here.

And then:

ob_end_clean(); // Stop buffering and dump everything (don't echo).
ob_end_flush(); // Stop buffering and echo out the buffer.
ob_get_clean(); // Stop buffering and return everything as a string.

Or any of the other related functions. I believe PHP calls ob_flush() at the end of a file if you don't.

萌酱 2024-10-30 11:53:35

当您之前有输出(header 函数)setcookie 时,您会收到此错误($name,$cookie,$cookie_expires);

确保之前没有任何 echoshtmltext 或任何内容(连空格都没有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 or html or text or anything(NOT EVEN A SPACE) before <?php of setcookie($name,$cookie,$cookie_expires );.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文