I am new to Angular 2 testing. I am trying to figure out what is the difference in using testsbed.get()
and just using inject
at the test level.
eg:
beforeEach(() => {
TestBed.configureTestingModule({
providers: [SomeService]
});
const testbed = getTestBed();
someService= testbed.get(SomeService);
});
});
vs
it('test service', inject([SomeService], (someService: SomeService) => {
inject
helper function was historically used since AngularJS as an alternative to direct injector calls. In Angular 1, it was necessary to bootstrap a test withngMock
. It is entirely optional in Angular 2 and higher and is just a suggested way for DI in TestBed tests.It a convenience wrapper for
testBed.get
that allows to avoid multipletestBed.get
calls, similarly to:Other helper functions can be optionally used in conjunction with
inject
, namelyasync
andfakeAsync
.