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

3

60

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) => {
Alonzoaloof answered 28/3, 2017 at 20:49 Comment(4)
I added Jasmine to tags and title for you. As it's the key technology this question relates to.Senescent
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.Brittnee
v4 github.com/angular/angular/blob/4.3.x/packages/core/testing/src/… / v2 github.com/angular/angular/blob/2.0.x/modules/%40angular/core/…Brittnee
@Brittnee AsyncTestCompleter has been used internally. It isn't a concern for end user, making inject efficiently the same thing as testBed.getBrockway
K
46

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

Koziara answered 21/2, 2020 at 12:53 Comment(1)
TestBed.inject was introduced in Angular v9, at the same time TestBed.get was deprecated.Outlet
B
29

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.

Brockway answered 21/7, 2017 at 4:37 Comment(0)
T
2

These used to be equivalent, but with Angular 9 the preferred method became inject().

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.

Traveled answered 9/4, 2021 at 21:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.