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
不客氣! :) –