Angular 测试无法读取 属性 'ngModule' 的 null
Angular Test Cannot read property 'ngModule' of null
我正在尝试在 Angular 上使用一个简单的测试,这里是测试的内容:
import { ComponentFixture } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';
import { By } from "@angular/platform-browser"
import { TestComponentComponent } from './test-component.component';
fdescribe('TestComponentComponent', () => {
let component: TestComponentComponent;
let fixture: ComponentFixture<TestComponentComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ TestComponentComponent ]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(TestComponentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should show no data', () => {
fixture.detectChanges();
const noDataDiv = fixture.debugElement.query(By.css('#noData'));
expect(noDataDiv.nativeElement.textContent).toEqual('No Data');
});
});
i 运行 通过这个命令测试:
ng 测试 --main .\src\app\test-component\test-component.component.spec.ts
我收到了这个错误:
TypeError: 无法读取 属性 'ngModule' of null
版本之间有什么变化吗?
我使用的是最新版本的angular,测试文件是用命令 ng generate component 生成的
我刚刚在describe后面加了'f'和最新的'it'
你不需要 --main
参数,你可以只 运行 文件包含:
ng test --include src/app/test-component/test-component.component.spec.ts
Include 也有 glob 支持,您可以使用它在文件夹中 运行 测试:
ng test --include src/**/*.spec.ts
--main
参数有什么用?
它用于定义 Karma Angular 应用程序的入口点。这很重要,因为 Angular 需要在测试模式下启动,例如可以创建模块和组件。
如果您查看项目中的 angular.json
,您会看到如下内容:
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts"
...
}
}
main
标识符链接到文件src/test.ts
,初始化测试环境:
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
因此 --main
的一个用例是为需要对测试环境进行特殊配置的不同类型的测试使用不同的入口点文件。
我正在尝试在 Angular 上使用一个简单的测试,这里是测试的内容:
import { ComponentFixture } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';
import { By } from "@angular/platform-browser"
import { TestComponentComponent } from './test-component.component';
fdescribe('TestComponentComponent', () => {
let component: TestComponentComponent;
let fixture: ComponentFixture<TestComponentComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ TestComponentComponent ]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(TestComponentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should show no data', () => {
fixture.detectChanges();
const noDataDiv = fixture.debugElement.query(By.css('#noData'));
expect(noDataDiv.nativeElement.textContent).toEqual('No Data');
});
});
i 运行 通过这个命令测试:
ng 测试 --main .\src\app\test-component\test-component.component.spec.ts
我收到了这个错误:
TypeError: 无法读取 属性 'ngModule' of null
版本之间有什么变化吗?
我使用的是最新版本的angular,测试文件是用命令 ng generate component 生成的
我刚刚在describe后面加了'f'和最新的'it'
你不需要 --main
参数,你可以只 运行 文件包含:
ng test --include src/app/test-component/test-component.component.spec.ts
Include 也有 glob 支持,您可以使用它在文件夹中 运行 测试:
ng test --include src/**/*.spec.ts
--main
参数有什么用?
它用于定义 Karma Angular 应用程序的入口点。这很重要,因为 Angular 需要在测试模式下启动,例如可以创建模块和组件。
如果您查看项目中的 angular.json
,您会看到如下内容:
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts"
...
}
}
main
标识符链接到文件src/test.ts
,初始化测试环境:
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
因此 --main
的一个用例是为需要对测试环境进行特殊配置的不同类型的测试使用不同的入口点文件。