Angular 角度单元测试:如何运行基本测试
我是角度单元测试的新手 我想实施第一个运行测试,因此我开发了以下内容:Angular 角度单元测试:如何运行基本测试,angular,unit-testing,typescript,jasmine,karma-runner,Angular,Unit Testing,Typescript,Jasmine,Karma Runner,我是角度单元测试的新手 我想实施第一个运行测试,因此我开发了以下内容: import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { AppComponent } from './app.component'; describe('AppComponent', () => { let component: AppComponent; let fixture: Component
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AppComponent ],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('first test', () => {
expect('1').toBe('1');
});
});
我确实在我的应用程序组件中使用了DevExtreme小部件,但我甚至没有尝试过测试它。我只是从明显的测试用例开始
我需要知道我应该如何修复它
建议???您必须在
测试床中包含编译组件所需的所有内容。configureTestingModule
:
// import { DevExtremeModule } from 'devextreme-angular';
import { DxProgressBarModule } from 'devextreme-angular/ui/progress-bar';
TestBed.configureTestingModule({
imports: [
DxProgressBarModule,
// or DevExtremeModule
],
declarations: [ AppComponent ],
}).compileComponents();
基本上,yurzui想说的是,您需要实际重新创建一个动态模块来测试您的组件。原因是,所有内容都与Angular stuff的依赖项注入相关联,您必须使用
TestBed.configureTestingModule()
重新创建一个模块,以提供组件编译所需的最低限度。如果您需要服务/管道或类似的东西,请不要忘记stub
将您的类用作模拟。@AlexBeugnet,非常感谢这些信息,因此我想知道如何对我的服务进行stub/mock??类似于{provide:MyService,useClass:myservicecomck}
,您声明的类应该覆盖现有的类,并使用模拟数据而不是HTTP调用。有关完整示例,请参见此链接:上一个链接中的锚点不太精确,因此这里是新的锚点:
// import { DevExtremeModule } from 'devextreme-angular';
import { DxProgressBarModule } from 'devextreme-angular/ui/progress-bar';
TestBed.configureTestingModule({
imports: [
DxProgressBarModule,
// or DevExtremeModule
],
declarations: [ AppComponent ],
}).compileComponents();