What is the difference between testbed.get and inject in Angular 2/Jasmine testing?

csaldanh picture csaldanh · Mar 28, 2017 · Viewed 15.2k times · Source

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) => {

Answer

Tom Maher picture Tom Maher · Feb 21, 2020

Just to add to the existing answer and if like me you found this question because you are wondering what the difference is between TestBed.get() and TestBed.inject() which I know was not quite what the OP originally asked but it is relevant and is very much related.

I thought it was worth posting that according to the latest Angular documentation that TestBed.inject() is the type safe replacement of TestBed.get().

From the Angular documentation on TestBed that can be found here.

enter image description here