2016-08-15 97 views
1

我正在嘗試爲我的應用運行集成測試。我有那些工作: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。有什麼辦法解決這個問題?

+0

你能告訴我們代碼嗎? –

+0

@AngadDubey我認爲代碼太簡單了,發佈它沒有意義。但是,當我一直在寫簡單的工作和測試時,我發現問題的根源。我修改了所有代碼示例和觀察結果 –

回答

0

對我來說,聽起來好像沒有dispatchNow()方法。在Jobs中運行dispatch(),但錯誤稱dispatchNow()不存在。

Laravel沒有在某個版本(我認爲Laravel 5.2 ...不確定)之前的dispatchNow()方法,而只是dispatch()。可能是因爲期望喬布斯沒有想到這一點,並失敗了。

你可以嘗試不通過它在一個數組,但使用3個命令:

$this->expectsJobs(StartJobs::class); 
$this->expectsJobs(PrepareJob::class); 
$this->expectsJobs(PerformJob::class); 

也許沒有什麼幫助。