42

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) => {
JGFMK
  • 7,107
  • 4
  • 46
  • 80
csaldanh
  • 451
  • 1
  • 4
  • 5
  • 1
    I added Jasmine to tags and title for you. As it's the key technology this question relates to. – JGFMK Jul 20 '17 at 09:30
  • 1
    I don't have a complete answer, but digging the code: `inject` calls `testbed.get` internally. The main difference is that if you provide `AsyncTestCompleter` to `inject` it will run `compileComponents` and the object provided has a `done` function that completes the execution of an async test. Looks like this is old code and it is preferred to use `async(inject(` instead. Then from this point looks like a syntax preference matter. – BrunoLM Jul 20 '17 at 20:37
  • 1
    v4 https://github.com/angular/angular/blob/4.3.x/packages/core/testing/src/test_bed.ts#L493 / v2 https://github.com/angular/angular/blob/2.0.x/modules/%40angular/core/testing/test_bed.ts#L404 – BrunoLM Jul 20 '17 at 20:39
  • 1
    @BrunoLM AsyncTestCompleter has been used internally. It isn't a concern for end user, making inject efficiently the same thing as testBed.get – Estus Flask Jul 21 '17 at 04:48

3 Answers3

31

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

Tom Maher
  • 1,584
  • 2
  • 19
  • 37
  • 1
    `TestBed.inject` was introduced in Angular v9, at the same time `TestBed.get` was deprecated. – nshew13 Dec 03 '20 at 04:11
28

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 with ngMock. 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 multiple testBed.get calls, similarly to:

const [foo, bar] = [Foo, Bar].map(TestBed.get);

Other helper functions can be optionally used in conjunction with inject, namely async and fakeAsync.

Estus Flask
  • 150,909
  • 47
  • 291
  • 441
0

These use to be equivalent, but in Angular 9 the preferred method was updated.

TestBed.get() is deprecated in Angular 9+, and TestBed.inject() is now the preferred type-safe way to inject a dependency.

Read the documentation for clarity: TestBed.get() and TestBed.inject(). The change is one of deprecation.

Tony Brasunas
  • 2,684
  • 3
  • 29
  • 35