我已将ng-select组件(https://github.com/ng-select/ng-select)导入到我的角应用程序中,并且在运行应用程序时不会抛出任何控制台错误,并按预期工作。但是,当使用Jasmine运行角度单位测试时会导致错误。
我已将NgSelectModule导入到imports数组下的app.module.ts文件中,并且我还通过声明数组将NgSelectComponent导入到我的configuration.component.spec.ts文件中:
import { NgSelectComponent } from '@ng-select/ng-select';
describe('Test Configuration Component',
() => {
let component: ConfigurationComponent;
let fixture: ComponentFixture<ConfigurationComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ConfigurationComponent, NgSelectComponent],
imports: [FormsModule],
providers: [{ provide: APIService, useClass: APIServiceMock }, Helper]
});
fixture = TestBed.createComponent(ConfigurationComponent);
component = fixture.componentInstance;
});
我的单元测试如下所示:
it('check that array of employees got binded to accounts', async () => {
fixture.detectChanges();
let employeeAccounts = [
{ "employeeAccountId": 1, "employeeId": 1, "name": "Name", "descriptor": "descriptor", "street": "Street", "postcode": "postcode", "state": "VIC", "city": "Melb", "country": "AU", "phone": "3123", "MCC": "MCC" },
{ "employeeAccountId": 2, "employeeId": 1, "name": "Name2", "descriptor": "descriptor2", "street": "Street2", "postcode": "postcode2", "state": "VIC2", "city": "Melb2", "country": "AU2", "phone": "3123", "MCC": "MCC2" }];
component.employeeAccounts = employeeAccounts;
component.ngOnInit();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(component.employeeAccounts[1]).toEqual(employeeAccounts[1]);
});
});
配置组件中ng-select的HTML如下所示:
<ng-select [(ngModel)]="selectedEmployeeAccountId" [items]="employeeAccounts" (change)="accountSelected()" [disabled]="isLoadingAccounts" [loading]="isLoadingAccounts" bindLabel="name" bindValue="employeeAccountId" placeholder="Please Select"></ng-select>
由于Angular 5兼容性问题,我不得不使用旧版本的ng-select(v 1.5.2)。
单元测试错误:
HeadlessChrome 0.0.0 (Windows 10 0.0.0) Test Configuration Component check that array of employees got binded to accounts FAILED
Error: Template parse errors:
Can't bind to 'virtualScroll' since it isn't a known property of 'ng-dropdown-panel'.
1. If 'virtualScroll' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.
2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
<ng-dropdown-panel *ngIf="isOpen"
class="ng-dropdown-panel"
[ERROR ->][virtualScroll]="virtualScroll"
[bufferAmount]="bufferAmount"
[appendTo]="appendTo"
"): ng:///DynamicTestModule/NgSelectComponent.html@47:4
Can't bind to 'bufferAmount' since it isn't a known property of 'ng-dropdown-panel'.
1. If 'bufferAmount' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.
2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
class="ng-dropdown-panel"
[virtualScroll]="virtualScroll"
[ERROR ->][bufferAmount]="bufferAmount"
[appendTo]="appendTo"
[position]="dropdownPosition"
"): ng:///DynamicTestModule/NgSelectComponent.html@48:4
Can't bind to 'appendTo' since it isn't a known property of 'ng-dropdown-panel'.
1. If 'appendTo' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.
2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
[virtualScroll]="virtualScroll"
[bufferAmount]="bufferAmount"
[ERROR ->][appendTo]="appendTo"
[position]="dropdownPosition"
[headerTemplate]="headerTemplate"
"): ng:///DynamicTestModule/NgSelectComponent.html@49:4
Can't bind to 'position' since it isn't a known property of 'ng-dropdown-panel'.
1. If 'position' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.
2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
[bufferAmount]="bufferAmount"
[appendTo]="appendTo"
[ERROR ->][position]="dropdownPosition"
[headerTemplate]="headerTemplate"
[footerTemplate]="footerTempl"): ng:///DynamicTestModule/NgSelectComponent.html@50:4
Can't bind to 'headerTemplate' since it isn't a known property of 'ng-dropdown-panel'.
1. If 'headerTemplate' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.
2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
[appendTo]="appendTo"
[position]="dropdownPosition"
[ERROR ->][headerTemplate]="headerTemplate"
[footerTemplate]="footerTemplate"
[items]="itemsList.filter"): ng:///DynamicTestModule/NgSelectComponent.html@51:4
Can't bind to 'footerTemplate' since it isn't a known property of 'ng-dropdown-panel'.
1. If 'footerTemplate' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.
2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
[position]="dropdownPosition"
[headerTemplate]="headerTemplate"
[ERROR ->][footerTemplate]="footerTemplate"
[items]="itemsList.filteredItems"
(update)="viewPortItems ="): ng:///DynamicTestModule/NgSelectComponent.html@52:4
Can't bind to 'items' since it isn't a known property of 'ng-dropdown-panel'.
1. If 'items' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.
2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
[headerTemplate]="headerTemplate"
[footerTemplate]="footerTemplate"
[ERROR ->][items]="itemsList.filteredItems"
(update)="viewPortItems = $event"
(scrollToEnd)="scrollToEn"): ng:///DynamicTestModule/NgSelectComponent.html@53:4
'ng-dropdown-panel' is not a known element:
1. If 'ng-dropdown-panel' is an Angular component, then verify that it is part of this module.
2. If 'ng-dropdown-panel' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
</span>
</div>
[ERROR ->]<ng-dropdown-panel *ngIf="isOpen"
class="ng-dropdown-panel"
[virtualScroll]="virtualScroll"
"): ng:///DynamicTestModule/NgSelectComponent.html@45:0
at syntaxError node_modules/@angular/compiler/esm5/compiler.js:486:22)
at TemplateParser.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.TemplateParser.parse node_modules/@angular/compiler/esm5/compiler.js:24674:1)
at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._parseTemplate node_modules/@angular/compiler/esm5/compiler.js:34629:1)
at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileTemplate node_modules/@angular/compiler/esm5/compiler.js:34604:1)
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/esm5/compiler.js:34505:48
at Set.forEach (<anonymous>)
at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileComponents node_modules/@angular/compiler/esm5/compiler.js:34505:1)
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/esm5/compiler.js:34393:1
at Object.then node_modules/@angular/compiler/esm5/compiler.js:475:33)
at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileModuleAndAllComponents node_modules/@angular/compiler/esm5/compiler.js:34391:1)
Failed: Cannot read property 'detectChanges' of undefined
TypeError: Cannot read property 'detectChanges' of undefined
at Object.<anonymous> src/app/configuration/configuration.component.spec.ts:87:17)
at step (http://localhost:9876/_karma_webpack_/main.bundle.js:89:23)
at Object.next (http://localhost:9876/_karma_webpack_/main.bundle.js:70:53)
at http://localhost:9876/_karma_webpack_/main.bundle.js:64:71
at new ZoneAwarePromise node_modules/zone.js/dist/zone.js:891:1)
at webpackJsonp../src/app/configuration/configuration.component.spec.ts.__awaiter (http://localhost:9876/_karma_webpack_/main.bundle.js:60:12)
at UserContext.<anonymous> src/app/configuration/configuration.component.spec.ts:86:66)
at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:388:1)
at ProxyZoneSpec.webpackJsonp../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke node_modules/zone.js/dist/zone-testing.js:288:1)
at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:387:1)
我相信这可能是因为我使用的ng-select版本没有导入所有必需的依赖项,但后来我在前端成功使用了下拉列表,所以我认为这可能是一个单独的问题。 / p>
识别问题的任何帮助都会很棒。
谢谢!
答案 0 :(得分:1)
将NgSelectModule添加到您的导入中,这为我修复了错误。
import {
NgSelectModule
} from '@ng-select/ng-select';
describe('Test Configuration Component',
() => {
let component: ConfigurationComponent;
let fixture: ComponentFixture < ConfigurationComponent > ;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ConfigurationComponent],
imports: [FormsModule, NgSelectModule],
providers: [{
provide: APIService,
useClass: APIServiceMock
}, Helper]
});
fixture = TestBed.createComponent(ConfigurationComponent);
component = fixture.componentInstance;
});
编辑:格式化。
答案 1 :(得分:0)
我遇到了同样的错误,还没有找到单独注入'ng-dropdown-panel'的方法。要解决此问题以进行测试,您可以设置架构:在测试台中设置[NO_ERRORS_SCHEMA],以防止发生此错误
按如下所示设置测试:
import { NO_ERRORS_SCHEMA } from '@angular/core';
describe('TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [FormsModule, ReactiveFormsModule],
declarations: [TestComponent, NgSelectComponent],
providers: [],
schemas:[NO_ERRORS_SCHEMA]
})
.compileComponents();
}));