0
正在爲我的組件執行簡單的單元測試。角度單元測試:失敗:模塊'DynamicTestModule'導入的意外值'DxTemplateHost':添加@NgModule註釋
我進口所需的一切:服務,RouterModuleTesting,FormsModule的HttpModule ...
想請注意,我用DevExtreme小工具。
但同樣,我面臨着一些奇怪的錯誤:
錯誤說我應該添加一個@NgModule annotion地方,但目前還不清楚。
我的測試文件如下:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CustomersListComponent } from './customers-list.component';
// DevExtreme Module
import {DxTemplateHost} from 'devextreme-angular';
// Router Testing Module
import {RouterTestingModule} from '@angular/router/testing';
// Services and HTTM Module
import { CustomerService } from './../customer.service';
import {HttpService} from '../../../shared/service/http.service';
import {SessionService} from '../../../shared/service/session.service';
import {HttpModule} from '@angular/http';
// Forms Module (ngModel)
import {FormsModule} from '@angular/forms';
// Schemas(datasource error)
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
describe('CustomerListComponent',() => {
let component: CustomersListComponent;
let fixture: ComponentFixture<CustomersListComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CustomersListComponent ],
imports: [
HttpModule,
FormsModule,
DxTemplateHost,
RouterTestingModule,
],
providers: [ CustomerService , SessionService , HttpService ],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CustomersListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('Customer-list component should be well defined',() => {
expect(component).toBeDefined();
});
});
當運行噶服務,測試falis並拋出這樣的:
Failed: Unexpected value 'DxTemplateHost' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
Error: Unexpected value 'DxTemplateHost' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
at syntaxError (webpack:///~/@angular/compiler/@angular/compiler.es5.js:1690:21 <- src/test.ts:117724:34)
at webpack:///~/@angular/compiler/@angular/compiler.es5.js:15383:0 <- src/test.ts:131417:44
at Array.forEach (<anonymous>)
at CompileMetadataResolver.Array.concat.CompileMetadataResolver.getNgModuleMetadata (webpack:///~/@angular/compiler/@angular/compiler.es5.js:15366:0 <- src/test.ts:131400:49)
at JitCompiler.Array.concat.JitCompiler._loadModules (webpack:///~/@angular/compiler/@angular/compiler.es5.js:26796:25 <- src/test.ts:142830:70)
at JitCompiler.Array.concat.JitCompiler._compileModuleAndAllComponents (webpack:///~/@angular/compiler/@angular/compiler.es5.js:26782:0 <- src/test.ts:142816:36)
at JitCompiler.Array.concat.JitCompiler.compileModuleAndAllComponentsAsync (webpack:///~/@angular/compiler/@angular/compiler.es5.js:26714:0 <- src/test.ts:142748:37)
at TestingCompilerImpl.Array.concat.TestingCompilerImpl.compileModuleAndAllComponentsAsync (webpack:///~/@angular/compiler/@angular/compiler/testing.es5.js:478:0 <- src/test.ts:232778:31)
at TestBed.Array.concat.TestBed.compileComponents (webpack:///~/@angular/core/@angular/core/testing.es5.js:739:0 <- src/test.ts:40380:31)
at Function.Array.concat.TestBed.compileComponents (webpack:///~/@angular/core/@angular/core/testing.es5.js:622:45 <- src/test.ts:40263:67)
建議?
爲什麼你想包括'DxTemplateHost'到'imports'陣列?它不是'@ NgModule',而是普通的類。我們只能導入由'@ NgModule'裝飾者裝飾的類 – yurzui
@yurzui:我做了這個cauz我不允許我使用DX模板語法 - >不能綁定到'dxTemplateOf',因爲它不是一個已知的屬性'div',我應該怎麼做呢? – firasKoubaa