如何使Laravel的通知在测试中引发异常

时间:2019-04-29 10:54:09

标签: php laravel testing mocking laravel-5.5

我有一些laravel命令从我自己的类继承,以在失败时发送松弛消息。但是,如果松弛通知失败,我仍然希望引发原始异常,以便如果松弛不可用或配置错误,错误仍然会在日志中结束。我已经有了它并且它可以工作,但是我无法弄清楚如何在测试的Notification部分中触发异常。

namespace App\Support;

use App\Notifications\SlackNotification;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Notification;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CarrotCommand extends Command
{   
    protected function execute(InputInterface $input, OutputInterface $output)                  
    {
        try {
            return parent::execute($input, $output);
        } catch (\Exception $e) {
            $this->notifySlack($e);

            throw $e;
        }
    }


    protected function notifySlack(\Exception $e)
    {
        try {
            Notification::route('slack', config('app.slack_webhook'))->notify(
                new SlackNotification(
                    "Error\n" . get_class($e) . ': ' . $e->getMessage(),
                    'warning'
                )
            ); 
        } catch (\Exception $exception) {

            // I want to reach this part in a test

            $this->error(
                'Failed to send notice to slack: ' . $exception->getMessage()
            );
        }
    }
}

由于Notification::route是在外观上定义的,因此我无法使用Notification::shouldReceive来触发异常,并且SlackNotification进行了更新,使其难以模拟。

关于如何触发异常的任何想法?

0 个答案:

没有答案
相关问题