简体   繁体   English

Angular单元测试中Testbed.inject(serviceName)和fixture.debugElement.injector.get(serviceName)的区别

[英]Difference between Testbed.inject(serviceName) and fixture.debugElement.injector.get(serviceName) in Angular Unit Testing

I was going through the test cases written by my team members.我正在浏览我的团队成员编写的测试用例。 What I observed was at some places they've used TestBed.inject(serviceName) for injecting the service and at some places they've used fixture.debugElement.injector.get(serviceName) .我观察到的是在某些地方他们使用TestBed.inject(serviceName)来注入服务,在某些地方他们使用了 fixture.debugElement.injector.get (serviceName) Can somebody tell me the difference between these two?有人能告诉我这两者的区别吗? Also what is the correct way of injecting services?还有什么是注入服务的正确方法?

For eg:例如:

let abc: ABCService让 abc: ABCService

Technique 1: abc = TestBed.inject(ABCService) Technique 2: abc = fixture.debugElement.injector.get(ABCSerice)技术 1:abc = TestBed.inject(ABCService) 技术 2:abc = fixture.debugElement.injector.get(ABCSerice)

Thanks in Advance提前致谢

I think they are essentially the same thing.我认为它们本质上是一样的。 I only use Technique 1 .我只使用Technique 1

In older versions of Angular, it was TestBed.get but now it is TestBed.inject .在旧版本的 Angular 中,它是TestBed.get但现在是TestBed.inject

I think TestBed.inject gets the service from the root injector while fixture.debugElement.injector.get gets the service that is actually injected into the component.我认为TestBed.inject从根注入器获取服务,而fixture.debugElement.injector.get获取实际注入组件的服务。

You can read more about it here .你可以在这里阅读更多关于它的信息。

You can provider a service in the Component decorator of a Component and each instance of this service will be unique (a separate instance) and I bet fixture.debugElement.inject.get will get this unique instance provided in the decorator as opposed to the global singleton.您可以在ComponentComponent装饰器中提供服务,并且该服务的每个实例都是唯一的(一个单独的实例),我敢打赌, fixture.debugElement.inject.get将获得装饰器中提供的这个唯一实例,而不是全局singleton。 Check out providers in decorators here .此处查看装饰器中的提供程序。

暂无
暂无

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

相关问题 Angular 单元测试 - new Service() 和 Testbed.inject() 有什么区别? - Angular Unit Test - What is the difference between new Service() and Testbed.inject()? Angular TestBed.inject - Angular TestBed.inject testbed.get 和 Angular 2/Jasmine 测试中的注入有什么区别? - What is the difference between testbed.get and inject in Angular 2/Jasmine testing? Angular 9 TestBed.inject 和提供者覆盖 - Angular 9 TestBed.inject & Provider Overrides 在单元测试中,fixture.debugElement.nativeElement.query 和 document.getElement 有什么区别? - In unit testing, what's the difference between fixture.debugElement.nativeElement.query and document.getElement? Angular 单元测试使用 TestBed.inject 根据测试执行顺序成功/失败 - Angular Unit Tests using TestBed.inject succeed/fail according to tests execution order 注入间谍时的 Angular TestBed.inject 类型错误 - Angular TestBed.inject type error when injecting into a spy 如果 TestBed.inject() 或 TestBed.get() (已弃用)之前运行过,则 TestBed.OverrideProvider() 不起作用 - TestBed.OverrideProvider() doesn't work if TestBed.inject() or TestBed.get() (deprecated) has run before Angular中的fixture.componentInstance和fixture.debugElement.componentInstance有什么区别? - What's the difference between fixture.componentInstance and fixture.debugElement.componentInstance in Angular? @Inject(Injector)注入器和注入器有什么区别? - what is the difference between @Inject(Injector) injector and injector?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM