【问题标题】:Want to unit test slack notification in laravel using PHPUnit想要使用 PHPUnit 在 laravel 中对 slack 通知进行单元测试
【发布时间】:2020-08-28 18:46:02
【问题描述】:

帮助我编写单元测试存根,用于发送或不发送松弛通知。在一个批次的执行中,如果发生错误,我会在松弛通道中发送错误。

【问题讨论】:

    标签: laravel notifications phpunit slack


    【解决方案1】:

    在测试中你应该使用 MockApplicationServices trait。它的工作原理与 the documented event mocking.

    用法是:

    $this->expectsNotification($notifiable, $notification);
    
    // eg.
    $this->expectsNotification($user, UpperLimitExceeded::class);
    

    还有 $this->withoutNotifications() 可以跳过任何遇到的通知。

    【讨论】:

    • 感谢您的回复!但是,当我的测试存根调用方法时,我仍然收到一个错误: $this->notify(new \App\Notifications\TestNotification($out, $slackChannelName));错误如下 1) Tests\Unit\ApplicationControllerTest::testBatch BadMethodCallException: Method Mockery_0_App_Http_Controllers_ApplicationController::getKey 不存在。
    • 有 1 次失败:1) Tests\Unit\ApplicationControllerTest::testBatch 以下预期通知未发送:[App\Http\Controllers\TestNotification] 下面是我的源代码:$this-> expectsNotification('test-slack',\App\Http\Controllers\TestNotification::class);]
    猜你喜欢
    • 2013-09-13
    • 2016-01-14
    • 2023-03-06
    • 2016-06-04
    • 2014-12-25
    • 2011-06-06
    • 2012-03-26
    • 2021-08-22
    • 1970-01-01
    相关资源
    最近更新 更多