如何在派發作業之後拋出異常的情況下測試作業參數。下面的測試返回綠色,但我沒有辦法測試工作參數。如何測試Laravel中的工作參數?
代碼:
<?php
namespace Tests\Feature;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Tests\TestCase;
class SomeJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle() {}
}
class NothingTest extends TestCase
{
/** @test */
public function dispatch_test()
{
$this->expectException(\Exception::class);
$this->expectsJobs(SomeJob::class);
// job dispatched and Exception thrown afterwards
dispatch(new SomeJob("argument to test"));
throw new \Exception();
}
}
你已經提到反思 - 我試圖找到沒有辦法獲得傳遞參數的值。它實際上可能嗎? –
我不認爲如果你不把它們分配給屬性,你實際上可以獲得通過參數。我已更新示例以顯示如何獲取私有屬性的值 –
我已經找到了一種方法來僅使用IOC容器測試參數而沒有反射。看看下面。好點嗎? –