Php 缓冲区是测试方法是否响应某些内容的正确方法吗?
假设有一个类“Debug”和一个“log”函数,该函数必须准确地输出传递给该函数的消息。有没有一个好的方法来测试它Php 缓冲区是测试方法是否响应某些内容的正确方法吗?,php,Php,假设有一个类“Debug”和一个“log”函数,该函数必须准确地输出传递给该函数的消息。有没有一个好的方法来测试它 class DebugTest extends PHPUnit_Framework_TestCase { public function test() { ob_start(); $d = new Debug; $d->log('foo'); $this->assertEquals(
class DebugTest extends PHPUnit_Framework_TestCase
{
public function test()
{
ob_start();
$d = new Debug;
$d->log('foo');
$this->assertEquals(
'foo',
ob_get_clean()
);
}
}
是否有替代方案?检查测试代码输出的(截至2017年)方法是使用以下功能:
在幕后,expectOutputString()
为您执行缓冲技巧该方法自PHPUnit 3.6.0(5年前发布)起就可用。检查测试代码输出的方法(截至2017年)是使用以下函数: 在幕后,
expectOutputString()
为您执行缓冲技巧该方法自PHPUnit 3.6.0(5年前发布)起就可用
class DebugTest extends PHPUnit_Framework_TestCase
{
public function test()
{
// Set the expectation
$this->expectOutputString('foo');
// Run the tested code
$d = new Debug;
$d->log('foo');
}
}