2014-03-07 25 views
0

我搜索的可能性,以使有關調度的Symfony2 phpspec一個functionnal測試phpspec,我想呼籲shouldBeCalled在調度

我想做到這一點:

$dispatcher->dispatch('workflow.post_extract', $event)->shouldBeCalled(); 

我的代碼是在這裏:

function it_should_dispatch_post_extract(
    EventDispatcher $dispatcher, GenericEvent $event, 
    TransformerInterface $transformer, ContextInterface $context, LoaderInterface $loader 
) 
{ 
    $c = new \Pimple([ 
     'etl' => new \Pimple([ 
      'e' => function() { 
       return new ExtractorMock(); 
      }, 
      't' => function() { 
       return new Transformer(); 
      }, 
      'l' => function() { 
       return new Loader(); 
      }, 
      'c' => function() { 
       return new Context(); 
      }, 
     ]) 
    ]); 

    $dispatcher->dispatch('workflow.post_extract', $event)->shouldBeCalled(); 

    $this->process($c); 
} 

phpspec的答案是:

! should dispatch post extract 
    method call: 
     Double\Symfony\Component\EventDispatcher\EventDispatcher\P10->dispatch("workflow.post_extract", Symfony\Component\EventDispatcher\GenericEvent:0000000043279e10000000004637de0f) 
    was not expected. 
    Expected calls are: 
     - dispatch(exact("workflow.post_extract"), exact(Double\Symfony\Component\EventDispatcher\GenericEvent\P11:0000000043279dce000000004637de0f)) 

回答

4

您在預期中傳遞的事件對象與傳遞到調度方法的事件對象不同。

它沒有錯,你只需要驗證它有點不同。別看一個具體實例,而是一類的任何實例:

$dispatcher->dispatch(
    'workflow.post_extract', 
    Argument::type('Symfony\Component\EventDispatcher\GenericEvent') 
)->shouldBeCalled(); 
+0

他對我說了同樣的錯誤: !應派遣提取 方法調用: Double \ Symfony \ Component \ EventDispatcher \ EventDispatcher \ P10-> dispatch(「workflow.post_transform」,Symfony \ Component \ EventDispatcher \ GenericEvent:0000000021772e420000000058f15b47) 不是預期的。 預期調用爲: - 調度(確切(「workflow.post_extract」),類型(Symfony \ Component \ EventDispatcher \ GenericEvent)) – user3393273

+0

這不一樣。注意事件名稱的不同(「workflow.post_transform」與「workflow.post_extract」)。你似乎在派遣不止一個事件。另外請注意,它不再期望具體的對象,而是一個類型。 –

+0

大分析,在進程函數調用中有效地有四個調度,在有調度事件post_extract後我必須添加其他調度post_transform並且它工作。 你認爲我可以在不同的功能測試中一對一地調用dispatch嗎? – user3393273