how to test all my Service are injected into component

Viewed 250

how to test all my Service are injected into component

class TestComponent {
    constructor(testService: TestService) {}
}

in case if anybody remove the service from component constructor my test case should fail.

1 Answers

We can make TestBed as a dummy Angular Module and we can configure it like one including with a set of providers,

TestBed.configureTestingModule({
  providers: [TestService]
});

Then resolve a token into a dependency using it’s internal injector and TestBed,

testBedService = TestBed.get(TestService);

Then your test case be like,

it('Service injected via inject(...) and TestBed.get(...) should be the same instance',
    inject([TestService], (injectService: TestService) => {
      expect(injectService).toBe(testBedService);
    })
);

Refer https://codecraft.tv/courses/angular/unit-testing/dependency-injection/

Related