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

后端 未结 2 557
无人及你
无人及你 2020-12-30 19:29

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.

相关标签:
2条回答
  • 2020-12-30 19:56

    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.

    0 讨论(0)
  • 2020-12-30 20:18

    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.

    0 讨论(0)
提交回复
热议问题