类型“ By”的参数不能分配给类型“ Predicate <DebugElement>”的参数

Wou*_*pen 1 jasmine typescript protractor angular

我正在尝试使用量角器(5.4.2),并尝试测试Angular组件。我创建了以下测试,这是从Joe Eames的Angular课程中单元测试启发而来的。

课程中的(有效)示例 在此处输入图片说明

我的(错误的)代码

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DetailsComponent } from './details.component';
import { By } from 'protractor';

describe('DetailsComponent', () => {
    let fixture: ComponentFixture<DetailsComponent>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [DetailsComponent]
        })
            .compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(DetailsComponent);

        fixture.detectChanges();
    });

     it('contains 3 titles', () => {
        expect(fixture.debugElement.queryAll(By.css('.card-title')).length).toBe(3);
    });
});
Run Code Online (Sandbox Code Playgroud)

但是,在测试方法中的.query调用中发生以下类型错误:

类型“ By”的参数不能分配给类型“ Predicate”的参数。

我不明白为什么会发生此错误,因为我的代码看起来像该示例。

Yev*_*kov 7

发生此错误是因为您是By从而protractor不是从@angular/platform-browser模块导入的。

将此更改import { By } from 'protractor';import { By } from '@angular/platform-browser';