FriendsOfBehat / SymfonyExtension

:musical_score: Extension integrating Behat with Symfony.
MIT License
472 stars 62 forks source link

Spy doesn't record calls unless test is the first to run #142

Closed gonzalovilaseca closed 1 year ago

gonzalovilaseca commented 3 years ago

We have a very strane behaviour here, we have this class:

class NotificationDispatcherSpy implements NotificationDispatcherInterface
{
    /** @var Message[] */
    private $calls = [];

    public function dispatch(Message $message)
    {
        $this->calls[] = $message;
    }

    /**
     * @return Message[]
     */
    public function getCalls(): array
    {
        return $this->calls;
    }
}

And it's injected both in Symfony and in a Behat context. Symfony makes the dispatch calls and then the context check for them with getCalls.

If the test that checks for the calls runs the first in the feature file, the calls are recorded as the spy is the same instance both for Symfony and Behat context.

If the test that uses the spy is not the first to run, the instance of the spy class that Symfony gets and the instance the Behat context gets are different and obviously the test fails because there are no recorded calls.

I've tried to debug the issue with no success, can anyone think of the reason? Thanks

mpdude commented 1 year ago

If this is still an issue for you, are you sure you're injecting/accessing the right service instance in the right kernel?

gonzalovilaseca commented 1 year ago

I'll close it as it's probably due to a diferent kernel being injected