#Mock nested services

7 messages · Page 1 of 1 (latest)

astral plume
#

Hey guys, quick question here:

It's possible to mock nested services? My point here is, i have UseCaseA that i'm writing some tests to it, this guy has ServiceAinjected in the constructor, and ServiceAhas ServiceBinjected.

I can mock ServiceA but ServiceB it's being loaded normally, but I would like to mock it too.

vast sable
#

If you mock ServiceA, then there would be no need to mock the nested one since there's nothing to inject it to. ServiceA has been replaced by a fske version.

#

Or are you possibly trying to test real instance of A in the UseCaseA and only moc the nested serviceB? That's also entirely possible via standard mocking syntax

#
{
   providers: [
      UseCaseA,
      {
         provide: ServiceA,
         useValue: mockedServiceA
      }
   ]
}
#

If you wish to write a test that uses both UseCaseA and serviceA, but mocks serviceB, follow the same pattern by including real classes in the test module and override those that should be mocked.

astral plume
#

ServiceA depends on some methods from ServiceB, so i think i would need to mock those methods too

vast sable
#

But if you're mocking service A, then nothing is using service B. It becomes entirely irrelevant for thre test.