phpspec,我想在调度员上调用shouldBeCalled

时间:2014-03-07 15:41:14

标签: php phpspec

我搜索了使用phpspec进行关于调度程序symfony2的功能测试的可能性

我想这样做:

$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))

1 个答案:

答案 0 :(得分:5)

您在期望中传递的事件对象与传递给dispatch方法的事件对象不同。

没什么不对的,你只需要稍微区别一点。不要查找特定的实例,而是查找类的任何实例:

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