gpt4 book ai didi

PHPUnit 测试总是成功,无论是 $this->once() 还是 $this->never()?

转载 作者:搜寻专家 更新时间:2023-10-31 21:14:15 27 4
gpt4 key购买 nike

我知道我做错了什么,因为这很奇怪,我几天前就开始学习 PHPUnit。测试对象是receive() Controller Action :

class ReportController
{
/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
private $dispatcher;

public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}

/**
* @param \Gremo\SkebbyBundle\Message\InboudSkebbyMessage $message
* @return \Symfony\Component\HttpFoundation\Response
*/
public function receive(InboudSkebbyMessage $message)
{
$this->dispatcher->dispatch(SkebbyEvents::MESSAGE_RECEIVED,
new InboundMessageEvent($message)
);

return new Response();
}
}

当 Controller 抛出一个事件时,我需要模拟一个事件订阅者(实现 Symfony\Component\EventDispatcher\EventSubscriberInterface )。有一个静态方法 getSubscribedEvents()。测试方法助手(获取模拟):

public function getMockSubscriber(array $events)
{
$class = $this->getMockClass(
'Symfony\Component\EventDispatcher\EventSubscriberInterface',
array_merge(array_values($events), array('getSubscribedEvents'))
);

// Static stub method
$class::staticExpects($this->once())
->method('getSubscribedEvents')
->will($this->returnValue($events))
;

return new $class;
}

然后在我的测试方法中,我注册了(模拟)订阅者,发出请求并检查 onMessageReceived() 是否被恰好调用了一次。 (实际上是大)问题是测试总是成功,即使我将模拟更改为 $subscriber->expects($this->never())->method('onMessageReceived' )。实现:

public function testApiCall()
{
$client = $this->createClient();

// Router (for route generation) and dispatcher (for subscribing the mock)
$router = $client->getContainer()->get('router');
$dispatcer = $client->getContainer()->get('event_dispatcher');

// Get mock event subscriber
$subscriber = $this->getMockSubscriber(array(
'messsage.received' => 'onMessageReceived'
));

// Register the mock subscriber with the dispatcher
$subscriber->expects($this->once())->method('onMessageReceived');
$dispatcer->addSubscriber($subscriber);

// Make the request
$request = Request::create(
$router->generate('controller_receive'),
'POST',
array(
'sender' => 'sender',
'receiver' => 'receiver',
'text' => 'text',
'timestamp' => time(),
'smsType' => 'smsType'
)
);

$client->getKernel()->handle($request);
}

编辑:模拟订阅者已正确注册到调度员。 var_dump($dispatcher->getListeners()):

array(7) {
'messsage.received' =>
array(1) {
[0] =>
array(2) {
[0] =>
class Mock_EventSubscriberInterface_19b191af#34 (2) {
...
}
[1] =>
string(17) "onMessageReceived"
}
}

最佳答案

我会使用模拟对象,而不是从类名创建一个新对象:

public function getMockSubscriber(array $events)
{
$subscriberMock = $this->getMock(
'Symfony\Component\EventDispatcher\EventSubscriberInterface',
array_merge(array_values($events), array('getSubscribedEvents'))
);

// Static stub method
$class = get_class(subscriberMock);
$class::staticExpects($this->once())
->method('getSubscribedEvents')
->will($this->returnValue($events))
;

return $subscriberMock;
}

关于PHPUnit 测试总是成功,无论是 $this->once() 还是 $this->never()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12623797/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com