TestBed.get 和 new Service(...dependencies) 有什么区别 [英] What's the difference between TestBed.get and new Service(...dependencies)

查看:26
本文介绍了TestBed.get 和 new Service(...dependencies) 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

angular guide 演示了两种不同的测试方式,一种是调用 new Service() 并直接将依赖项提供给构造函数,然后通过调用 TestBed.get(Service) 使用依赖注入.

The angular guide demonstrates two different ways of testing, one by calling new Service() and providing the dependencies to the constructor directly, and the second using dependency injection by calling TestBed.get(Service).

这两者在功能上似乎与我相同,除了当我连续调用 TestBed.get() 时,它不会在第一次调用后调用构造函数.

Both of these seem functionally identical to me, except when I call TestBed.get() consecutively it does not call the constructor after the first call.

角度文档还提到 TestBed.get() 已弃用(即使指南仍然引用它!)并且我应该使用 TypeInjectionToken 相反,但我看不出这些类中的任何一个如何替换 TestBed.get().

The angular documentation also mentions that TestBed.get() is deprecated (even though the guide still references it!) and that I should use Type or InjectionToken instead, but I do not see how either of these classes could replace TestBed.get().

推荐答案

当你调用 TestBed.configureTestingModule({ providers: [SomeService] }); 时,这会设置一个可以使用的 NgModule在随后的测试中.如果您调用 TestBed.get(SomeService),这将从注入器中检索 SomeService 并在需要时将其实例化.如果它被实例化了,那么注入器会注入对其依赖项的引用并返回一个新的服务实例.

When you call TestBed.configureTestingModule({ providers: [SomeService] });, this sets up an NgModule that can be used in subsequent tests. If you call TestBed.get(SomeService), this retrieves SomeService from the injector and instantiates it if needed. If it is instantiated, then the injector injects references to it's dependencies and returns a new instance of the service.

如果 SomeService 已经被实例化,就像你的情况一样,那么 TestBed 不需要创建它.这意味着它不会在以后调用构造函数.

If SomeService has already been instantiated, as in your case, then the TestBed does not need to create it. This means that it won't call the constructor a subsequent time.

要回答您关于差异的问题,如果您要模拟所有依赖项并且不需要访问 DOM,则它们基本上是相同的.在没有 TestBed 的情况下实例化类要快得多,因为没有为每个测试加载依赖注入器的开销.

To answer your question about the difference, basically they are the same if you are mocking all of your dependencies and if you don't need to access the DOM. Instantiating classes without the TestBed is significantly faster because there isn't the overhead of loading the dependency injector for every test.

至于 TestBed.get() 被弃用,在 Angular 8.0.0 中,只有允许任何类型的特定重载被弃用(参见 https://github.com/angular/angular/blob/master/packages/core/testing/src/test_bed.ts#L67).而不是 get(token: any, notFoundValue?: any): any; 签名改为 get 这意味着你必须使用类引用或注入令牌.没有字符串或其他东西可以引用注入器中的东西.

As for the TestBed.get() being deprecated, in Angular 8.0.0, only the specific overload that allows any type was deprecated (see https://github.com/angular/angular/blob/master/packages/core/testing/src/test_bed.ts#L67). Instead of get(token: any, notFoundValue?: any): any; the signature was changed to get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any; which means that you had to use a class reference or injection token. No strings or other things to reference something in the injector.

在 Angular 9.0.0 中,TestBed.get() 方法将被完全弃用,您需要改用 TestBed.inject.请参阅 https://github.com/angular/angular/blob/master/packages/core/testing/src/test_bed.ts#L65

In Angular 9.0.0, the TestBed.get() method will be fully deprecated and you will need to use TestBed.inject instead. See https://github.com/angular/angular/blob/master/packages/core/testing/src/test_bed.ts#L65

这篇关于TestBed.get 和 new Service(...dependencies) 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆