我正在嘗試爲我的應用運行集成測試。我有那些工作:Laravel集成測試工作
- StartJob
- PrepareJob
- PeformJob
StartJob分派一個或多個PrepareJob,每PrepareJob分派一個PerformJob。
添加此
$this->expectsJobs(
[
StartJobs::class,
PrepareJob::class,
PerformJob::class
]
);
讓我的測試失敗,出現錯誤說
1) JobsTest::testJobs
BadMethodCallException: Method Mockery_0_Illuminate_Contracts_Bus_Dispatcher::dispatchNow() does not exist on this mock object
刪除$這個 - > expectsJobs讓我所有的測試都通過了,但我不能斷言一個給定的作業運行,只是它是否修改了數據庫到一個給定的狀態。
StartJobs.php
class StartJobs extends Job implements ShouldQueue
{
use InteractsWithQueue;
use DispatchesJobs;
public function handle(Writer $writer)
{
$writer->info("[StartJob] Started");
for($i=0; $i < 5; $i++)
{
$this->dispatch(new PrepareJob());
}
$this->delete();
}
}
PrepareJob.php
class PrepareJob extends Job implements ShouldQueue
{
use InteractsWithQueue;
use DispatchesJobs;
public function handle(Writer $writer)
{
$writer->info("[PrepareJob] Started");
$this->dispatch(new PerformJob());
$this->delete();
}
}
PerformJob.php
class PerformJob extends Job implements ShouldQueue
{
use InteractsWithQueue;
public function handle(Writer $writer)
{
$writer->info("[PerformJob] Started");
$this->delete();
}
}
JobsTest.php
class JobsTest extends TestCase
{
/**
* @var Dispatcher
*/
protected $dispatcher;
protected function setUp()
{
parent::setUp();
$this->dispatcher = $this->app->make(Dispatcher::class);
}
public function testJobs()
{
$this->expectsJobs(
[
StartJobs::class,
PrepareJob::class,
PerformJob::class
]
);
$this->dispatcher->dispatch(new StartJobs());
}
}
我認爲它必須做一些我如何使用具體的調度程序,而$ this-> expectsJob嘲笑調度員。可能與此有關 - https://github.com/laravel/lumen-framework/issues/207。有什麼辦法解決這個問題?
你能告訴我們代碼嗎? –
@AngadDubey我認爲代碼太簡單了,發佈它沒有意義。但是,當我一直在寫簡單的工作和測試時,我發現問題的根源。我修改了所有代碼示例和觀察結果 –