Answer the question
In order to leave comments, you need to log in
How to correctly describe the specification (phpspec)?
Good afternoon! I'm trying to get into testing, namely SpecBDD (PHPSpec). There was a small question. There is a run()
method (it is clear that there should not be a code specification before writing the code specification, it is just for an example):
public function run()
{
/** @var EventDispatcherInterface $eventDispatcher */
$eventDispatcher = $this->container
->get('event_dispatcher');
$eventDispatcher->dispatch(static::EVENT_RUN);
if ($this->request) {
$request = $this->request;
} else {
$request = Request::createFromGlobals();
}
$requestEvent = new RequestEvent($request);
$eventDispatcher->dispatch(static::EVENT_REQUEST, $requestEvent);
if (!$requestEvent->getResponse()) {
throw new NoResponseException();
}
$responseEvent = new ResponseEvent($requestEvent->getRequest(), $requestEvent->getResponse());
$eventDispatcher->dispatch(static::EVENT_RESPONSE, $responseEvent);
$responseEvent->getResponse()
->send();
}
Answer the question
In order to leave comments, you need to log in
The whole relish of PhpSpec is manifested just in such cases. Do you find it difficult to get wet? Is it difficult to write tests? So something went wrong with your code (PhpSpec is very restrictive in how and what you can test).
1) why are you injecting the entire container? Inject - then mock it and in the get method return a mock for the event dispatcher
2) everything that is created inside the method is not mocked. That is, since you are doing new in the method, then it is necessary. You won't do anything here. And since we have a mock event dispatcher, you can check what exactly you are sending there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question