我正在为一个mat-stepper写一个测试。 我遇到了错误。错误: Export of name 'matHorizontalStepper' not found!
多亏了下面的评论,我弄明白了。
当使用Angular Material组件进行测试时,你必须在测试文件中导入它们,同时导入BrowserAnimationsModule,有时导入ReactiveFormsModule。
describe('EditFulfillmentWorkflowComponent', () => {
let component: EditFulfillmentWorkflowComponent;
let fixture: ComponentFixture<EditFulfillmentWorkflowComponent>;
beforeEach(() => {
const activatedRouteStub = () => ({
snapshot: { paramMap: { get: () => ({}) } }
});
const routerStub = () => ({ navigateByUrl: string => ({}) });
const notificationsServiceStub = () => ({});
const fulfillmentServiceStub = () => ({
getFulfillmentWorkflows: any => ({ subscribe: f => f }),
getOrchestratorActionInfo: any => ({ subscribe: f => f }),
checkIfWorkflowNameExists: any => ({ subscribe: f => f(Boolean) }),
updateFulfillmentWorkflow: submitFulfillmentWorkflow => ({
subscribe: f => f({})
})
});
TestBed.configureTestingModule({
schemas: [NO_ERRORS_SCHEMA],
declarations: [EditFulfillmentWorkflowComponent],
imports: [MatStepperModule, BrowserAnimationsModule, ReactiveFormsModule],
providers: [
{ provide: ActivatedRoute, useFactory: activatedRouteStub },
{ provide: Router, useFactory: routerStub },
{ provide: NotificationsService, useFactory: notificationsServiceStub },
{ provide: FulfillmentService, useFactory: fulfillmentServiceStub }
]
});
fixture = TestBed.createComponent(EditFulfillmentWorkflowComponent);
component = fixture.componentInstance;
});
beforeEach(() => {
fixture = TestBed.createComponent(EditFulfillmentWorkflowComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
编辑】第一条评论解决了我的问题。
首先,你需要导入你的组件使用的所有模块。
正因为如此,你需要导入。
import {MatStepperModule} from '@angular/material/stepper';
并将其添加到... imports
之中 TestBed.configureTestingModule({...})
另外,如果你在你的组件中使用了一个组件,你需要在你的组件中加入 declarations: [EditFulfillmentWorkflowComponent],
的 TestBed.configureTestingModule({...})