Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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) => {
like image 713
csaldanh Avatar asked Mar 28 '17 20:03

csaldanh


People also ask

What is TestBed in Jasmine?

TestBed is a mock environment to run Angular2 component tests without the browser.

What is TestBed inject?

TestBed. configureTestingModule() helps you configure the providers. Configuring the providers means you are letting the Angular dependency injection system know about this dependency which later it can inject in to components when requested through a dependency injection token.

What is TestBed in Angular unit testing?

The TestBed is the first and largest of the Angular testing utilities. It creates an Angular testing module — a @NgModule class — that you configure with the configureTestingModule method to produce the module environment for the class you want to test.

Which TestBed method is used to create an Angular component under test?

The TestBed. createComponent() method is used to create an instance of the AppComponent. The spec then uses expect and matcher functions to see if the component produces the expected behavior. As a result, the spec will either pass or fail.


Video Answer


3 Answers

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

like image 138
Tom Maher Avatar answered Oct 18 '22 21:10

Tom Maher


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.

like image 25
Estus Flask Avatar answered Oct 18 '22 20:10

Estus Flask


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.

like image 2
Tony Brasunas Avatar answered Oct 18 '22 21:10

Tony Brasunas