用phpspec测试输出缓冲

时间:2018-10-24 15:05:09

标签: phpspec

给出以下PHPUnit代码,如何将其转换为phpspec测试?

$content = 'Hello world!';
ob_start();
$this->displayer->output($content);
$output = ob_get_clean();
$this->assertEquals($content, $output);

$this->displayer->output($content)所做的只是echo $content

class Displayer {
    public function display(string $content) { echo $content; }
}

1 个答案:

答案 0 :(得分:1)

我相信这是唯一的方法:

use PHPUnit\Framework\Assert;

public function it_outputs_a_string()
{
    $content = 'Hello world!';
    ob_start();      
    $this->display($content);
    $output = ob_get_clean();

    Assert::assertEquals($content, $output);
}