亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 無法使用模擬的服務和對象測試作業。Mockery 間諜無法檢測到被調用的方法

Laravel 無法使用模擬的服務和對象測試作業。Mockery 間諜無法檢測到被調用的方法

PHP
慕田峪7331174 2022-10-28 09:36:05
我想測試我的工作功能:namespace App\Jobs;use Illuminate\Bus\Queueable;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Foundation\Bus\Dispatchable;use Illuminate\Queue\InteractsWithQueue;use App\Model\Model;use App\Services\Service;class MyJob implements ShouldQueue{    use Dispatchable;    use InteractsWithQueue;    use Queueable;    /**     * Undocumented variable     *     * @var User     */    private $model;    public function __construct(Model $model)    {        $this->model=$model;    }    public function handle(Service $service): void    {        if($this->model->shouldCallService){            $service->method();        }    }}我想用一個純單元測試來測試它:namespace Tests\Jobs;use Tests\TestCase;use Mockery;use App\Services\MyService;use App\Jobs\MyJob;use App\Model\MyModel;class TestJob extends TestCase{    public function testMethod()    {                $this->app->instance(MyModel::class,\Mockery::mock(MyModel::class,function($mock){            $mock->shouldReceive('save')->andReturn(true);        }));        $model=factory(MyModel::class)->create([            'shouldCallService'=>true,        ]);        $mockedNewsLetterService=Mockery::spy(MyService::class);        $mockedNewsLetterService->shouldReceive('method')->once();        $job=new MyJob($model);        $job->handle($mockedNewsLetterService);        $mockedNewsLetterService->shouldHaveReceived()->method()->with($model);    }    public function tearDown() {        parent::tearDown();        Mockery::close();    }}但是一旦我通過以下方式測試它:./vendor/bin/phpunit ./test/Jobs/TestJob.php我得到以下輸出:PHPUnit 7.5.20 by Sebastian Bergmann and contributors.E                                                                   1 / 1 (100%)Time: 46.12 seconds, Memory: 34.00 MBThere was 1 error:ERRORS!Tests: 1, Assertions: 3, Errors: 1.你知道為什么間諜無法檢測到調用method方法嗎?
查看完整描述

1 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

你錯誤地稱之為shouldHaveReceived嘲弄。


使用這種方法:


$mockedNewsLetterService->shouldHaveReceived(`method`)->with($model);

此外,為了測試我使用的作業調度bindMethod,app因此測試應該是:


public function testMethod()

{        

    $this->app->instance(MyModel::class,\Mockery::mock(MyModel::class,function($mock){

        $mock->shouldReceive('save')->andReturn(true);

    }));


    $model=factory(MyModel::class)->create([

        'shouldCallService'=>true,

    ]);


     $mockedNewsLetterService=Mockery::spy(MyService::class);


    $this->app->bindMethod(NewsLetterSubscribeUserJob::class.'@handle', function ($job, $app) use ($mockedNewsLetterService){

        return $job->handle($mockedNewsLetterService);

    });


    MyJob::dispatchNow($model);

    $mockedNewsLetterService->shouldHaveReceived('method')->with($model);

}

如您所見,我使用該應用程序來處理作業調度。另外,為了避免在測試期間使用第 3 方服務,我也使用dispatchNow了這樣的服務,以便我的工作不被排隊。


查看完整回答
反對 回復 2022-10-28
  • 1 回答
  • 0 關注
  • 126 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號