I'm curious about whether I'm doing things right here, as I'm new to testing.
I have two services (so far), AuthService
and CommentService
. CommentService
depends on AuthService
, and AuthService
depends on a 3rd party class (\SlimSession\Helper
).
In my unit test for AuthService
I simply mock \SlimSession\Helper
and provide the mock to the constructor.
Now, in my test for CommentService
I mock the AuthService
and provide it to the constructor of the CommentService
. But I must also create a mock of the \SlimSession\Helper
again to provide it to the constructor of my mocked AuthService
.
$session = $this->getMockBuilder('\SlimSession\Helper')
->getMock();
$auth = $this->getMockBuilder('\App\Service\AuthService')
->setConstructorArgs([$session])
->getMock();
$auth->expects($this->any())->method('isLoggedIn')->willReturn(false);
$auth->expects($this->any())->method('getLoggedInUserId')->willReturn(0);
Is this right? It seems a bit silly to have to provide a (mock) dependency to a mock object, since it won't use that dependency anyway.
My question regards PHPUnit
in particular, but I guess it could be made generally for unit testing.
Aucun commentaire:
Enregistrer un commentaire