注入不能使用Angular2的Jasmine测试(承诺不会浮动)

时间:2016-06-03 20:59:35

标签: angular jasmine

因此,我尝试使用规范测试一些代码。

import {beforeEachProviders, inject} from '@angular/core/testing';
import {TestComponentBuilder} from '@angular/compiler/testing';

describe('TestComponent', () => {
    it('should fail', inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
        expect(1).toBe(2);
    }));
});

这会导致错误: 没有TestComponentBuilder的提供者!

我在下面进行了一些健全检查测试,但是它们有效:

it('true is true', () => expect(true).toEqual(true));
it('null is not the same thing as undefined',
      () => expect(null).not.toEqual(undefined)
    );

在PyCharm中我注入了一个错误,它告诉我: 类型'功能'的论证不能分配给'(已完成:DoneFn)=>类型的参数无效&#39 ;.输入'功能'不提供签名匹配'(已完成:DoneFn):void'。

感谢任何帮助,我预计我正在使用一些旧的注入实现但我无法找到新的文档:)

1 个答案:

答案 0 :(得分:4)

我在beforeEach()语句中遇到了同样的错误。你需要导入"它"来自@angular/core/testing。 Jasmine的基本功能无法接收inject()的返回。

import {beforeEachProviders, inject, it} from '@angular/core/testing';

以上就足够了。

我在该问题中找到了解决方案:https://stackoverflow.com/a/35589775/2683681