简体   繁体   English

testbed.get 和 Angular 2/Jasmine 测试中的注入有什么区别?

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

I am new to Angular 2 testing.我是 Angular 2 测试的新手。 I am trying to figure out what is the difference in using testsbed.get() and just using inject at the test level.我试图弄清楚使用testsbed.get()和只在测试级别使用inject什么区别。

eg:例如:

beforeEach(() => {
    TestBed.configureTestingModule({
        providers: [SomeService]
    });

    const testbed = getTestBed();
    someService= testbed.get(SomeService);
  });
});

vs对比

it('test service', inject([SomeService], (someService: SomeService) => {

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.只是为了添加到现有答案,如果像我一样,你发现了这个问题,因为你想知道TestBed.get()TestBed.inject()之间有什么区别,我知道这与 OP 最初要求的不太一样,但它是相关的并且非常相关。

I thought it was worth posting that according to the latest Angular documentation that TestBed.inject() is the type safe replacement of TestBed.get() .我认为这是值得发帖称根据最新的角度说明文件TestBed.inject()是类型安全的替换TestBed.get()

From the Angular documentation on TestBed that can be found here .从可以在此处找到的TestBed上的 Angular 文档。

在此处输入图片说明

inject helper function was historically used since AngularJS as an alternative to direct injector calls.自 AngularJS 以来, inject辅助函数一直被用作直接注入器调用的替代方法。 In Angular 1, it was necessary to bootstrap a test with ngMock .在 Angular 1 中,有必要使用ngMock引导测试。 It is entirely optional in Angular 2 and higher and is just a suggested way for DI in TestBed tests.它在 Angular 2 及更高版本中完全是可选的,并且只是 TestBed 测试中 DI 的一种建议方式。

It a convenience wrapper for testBed.get that allows to avoid multiple testBed.get calls, similarly to: 它是testBed.get的便利包装器,可以避免多次testBed.get调用,类似于:

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

Other helper functions can be optionally used in conjunction with inject , namely async and fakeAsync .其他辅助函数可以选择性地与inject结合使用,即asyncfakeAsync

These use to be equivalent, but in Angular 9 the preferred method was updated.这些用法是等效的,但在 Angular 9 中更新了首选方法。

TestBed.get() is deprecated in Angular 9+, and TestBed.inject() is now the preferred type-safe way to inject a dependency. TestBed.get()在 Angular 9+ 中已被弃用,而TestBed.inject()现在是注入依赖项的首选类型安全方式。

Read the documentation for clarity: TestBed.get() and TestBed.inject() .为清楚起见,请阅读文档: TestBed.get()TestBed.inject() The change is one of deprecation.更改是弃用之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TestBed.get 和 new Service(...dependencies) 有什么区别 - What's the difference between TestBed.get and new Service(...dependencies) Angular单元测试中Testbed.inject(serviceName)和fixture.debugElement.injector.get(serviceName)的区别 - Difference between Testbed.inject(serviceName) and fixture.debugElement.injector.get(serviceName) in Angular Unit Testing 如果 TestBed.inject() 或 TestBed.get() (已弃用)之前运行过,则 TestBed.OverrideProvider() 不起作用 - TestBed.OverrideProvider() doesn't work if TestBed.inject() or TestBed.get() (deprecated) has run before Angular 单元测试 - new Service() 和 Testbed.inject() 有什么区别? - Angular Unit Test - What is the difference between new Service() and Testbed.inject()? 如果我先前校准过testBed.get(),是否必须在单元测试中注入服务? - Do I have to inject the service in the unit test if I cal testBed.get() previously? Angular TestBed.inject - Angular TestBed.inject 什么是Jasmine的TestBed - What is TestBed in Jasmine Angular2 TestBed注入 - Angular2 TestBed Inject Angular 6 和 Jest 的测试出错 - TypeError: testing.TestBed.inject is not a function - Error in tests with Angular 6 and Jest - TypeError: testing.TestBed.inject is not a function TestBed 上的提供者和声明之间有什么区别 - What is the difference between providers and declarations on TestBed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM