在Angular 2 / Jasmine测试中testbed.get和inject有什么区别? [英] What is the difference between testbed.get and inject in Angular 2/Jasmine testing?

查看:205
本文介绍了在Angular 2 / Jasmine测试中testbed.get和inject有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular 2测试的新手。我试图弄清楚在测试中使用 testsbed.get()和只使用注入有什么区别等级。

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.

例如:

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

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

vs

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


推荐答案

注入自从AngularJS作为直接注入器调用的替代方案以来,历史上使用了辅助函数。在Angular 1中,它是使用 ngMock 引导测试所必需的。在Angular 2及更高版本中它是完全可选的,并且只是在TestBed测试中建议的DI方式。

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.

它是 testBed.get 的便利包装器,允许避免多个 testBed.get 调用,类似于:

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

其他辅助函数可以选择与 inject 一起使用,即 async an d fakeAsync

Other helper functions can be optionally used in conjunction with inject, namely async and fakeAsync.

这篇关于在Angular 2 / Jasmine测试中testbed.get和inject有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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