Inject无法与Angular2的Jasmine测试配合使用(承诺没有实现)

Cor*_*rey 1 jasmine angular

因此,我有一些代码正在尝试使用规范进行测试。

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);
    }));
});
Run Code Online (Sandbox Code Playgroud)

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

我下面有一些健全性检查测试,它们可以正常工作:

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

在PyCharm中,我在注入时遇到错误,它告诉我:类型'Function'的参数不能分配给类型'(done:DoneFn)=> void'的参数。类型'Function'不提供签名'(done:DoneFn):void'的匹配项。

感谢您的任何帮助,我希望我使用的是inject的一些旧实现,但找不到新文档:)

小智 5

我在使用beforeEach()语句时遇到了同样的错误。您需要从中导入“它” @angular/core/testing。Jasmine的基本功能无法接收inject()的返回。

import {beforeEachProviders, inject, it} from '@angular/core/testing';
Run Code Online (Sandbox Code Playgroud)

以上就足够了。

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