2
我想知道是否有可能在Laravel测试ShouldBroadcast
? 我的测试会触发甚至执行ShouldBroadcast
,但是,我注意到广播没有发生。如何测试广播驱动程序?
当然,广播事件正在它自己的应用程序中工作。
有没有人试过这样的测试?
我想知道是否有可能在Laravel测试ShouldBroadcast
? 我的测试会触发甚至执行ShouldBroadcast
,但是,我注意到广播没有发生。如何测试广播驱动程序?
当然,广播事件正在它自己的应用程序中工作。
有没有人试过这样的测试?
这是一个古老的问题,但以防万一它可以帮助某人:我为自己做了一个断言函数,您应该添加到您的TestCase.php。
这个想法是将广播驱动程序设置为“日志”,然后从结尾读取第4行以查看它是否是广播日志。
在你phpunit.xml
如下因素行添加到节点:
<env name="BROADCAST_DRIVER" value="log"/>
在你TestCase.php文件
添加如下因素的功能:
public function assertEventIsBroadcasted($eventClassName, $channel=""){
$logfileFullpath = storage_path("logs/laravel.log");
$logfile = explode("\n", file_get_contents($logfileFullpath));
if(count($logfile) > 4){
$supposedLastEventLogged = $logfile[count($logfile)-5];
$this->assertContains("Broadcasting [", $supposedLastEventLogged, "No broadcast were found.\n");
$this->assertContains("Broadcasting [".$eventClassName."]", $supposedLastEventLogged, "A broadcast was found, but not for the classname '".$eventClassName."'.\n");
if($channel != "")
$this->assertContains("Broadcasting [".$eventClassName."] on channels [".$channel."]", $supposedLastEventLogged, "The expected broadcast (".$eventClassName.") event was found, but not on the expected channel '".$channel."'.\n");
}else{
$this->fail("No informations found in the file log '".$logfileFullpath."'.");
}
}
谢谢s回答:) – simo
不客气! :) –