mardi 26 mars 2019

How to properly test a service method that uses other services

I'm stuck on how to properly test a method on a Symfony service which only purpose is to call other methods on other services.

Trying to unit test the method by mocking every depencency, and relative methods, it uses, just makes the test useless in my opinion, since what i'm testing, in a way, is just that the other services are called correctly. Also, as i read on various sources, i shouldn't mock what i don't own, and basically all such methods use at least one (EntityManager, EncoderFactory, ecc...)

Trying to function test it with booting the kernel, grabbing the service and calling the method was a nightmare, and i got stuck on asserting that emails was sent, since i need a client and a response to grab all the emails that've been sent from the profiler.

This is an example of such methods that i have to test:

public function postRequestCreatedActions(PrivacyRequest $request, $sendNotifications = true)
{
    $this->customLogger->logRequest($request);
    if ($sendNotifications) {
        $this->customMailer->sendRequestCreated($request);
    }
    $this->em->flush();
}

So, my question is: if there's a way to properly test methods like this (unit or functional), how should i test it? If a method like this is untestable, and needs to be changed, or removed entirely, how do you advice to refactor it without clogging the controller that calls it (each one is called by a controller)? Or is moving all this logic to the controller the only way?

Thanks

Aucun commentaire:

Enregistrer un commentaire