When you call TestBed.configureTestingModule({ providers: [SomeService] });
, this sets up an NgModule that can be used in subsequent tests. If you call TestBed.get(SomeService)
, this retrieves SomeService
from the injector and instantiates it if needed. If it is instantiated, then the injector injects references to it's dependencies and returns a new instance of the service.
If SomeService
has already been instantiated, as in your case, then the TestBed does not need to create it. This means that it won't call the constructor a subsequent time.
To answer your question about the difference, basically they are the same if you are mocking all of your dependencies and if you don't need to access the DOM. Instantiating classes without the TestBed is significantly faster because there isn't the overhead of loading the dependency injector for every test.
As for the TestBed.get() being deprecated, in Angular 8.0.0, only the specific overload that allows any type was deprecated (see https://github.com/angular/angular/blob/master/packages/core/testing/src/test_bed.ts#L67). Instead of get(token: any, notFoundValue?: any): any;
the signature was changed to get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
which means that you had to use a class reference or injection token. No strings or other things to reference something in the injector.
In Angular 9.0.0, the TestBed.get() method will be fully deprecated and you will need to use TestBed.inject
instead. See https://github.com/angular/angular/blob/master/packages/core/testing/src/test_bed.ts#L65