我正在编写测试以检查子组件发出一些数据时是否调用了父组件,这是我设置测试组件的方法:
describe('OperationComponent', () => {
let component: OperationComponent;
let fixture: ComponentFixture<OperationComponent>;
let store: Store;
let de: DebugElement;
let data: any;
let cmp: any;
beforeEach(async(() => {
const {
imports,
providers
} = configureDefaultTestingMouduleForSecureComponent(OperationComponent);
TestBed.resetTestingModule();
TestBed.configureTestingModule({
declarations: [OperationComponent],
imports: [...imports, NgxsModule.forFeature([RoutesOperationState])],
providers: [...providers, RoutesOperationEntity]
}).compileComponents();
store = TestBed.get(Store);
}));
beforeEach(() => {
fixture = TestBed.createComponent(OperationComponent);
component = fixture.componentInstance;
de = fixture.debugElement;
data = de.query(By.directive(RouteOperationsComponent));
cmp = data.componentInstance;
component.routeId = mockRoutes[0].id;
});
describe('cancel action', () => {
it('should be called on cancelOperation when it emits data', () => {
jest.spyOn(component, 'handleCancelRouteOperation');
cmp.cancelRouteOperation.emit(true);
expect(component.handleCancelRouteOperation).toHaveBeenCalledWith(true);
});
});
configureDefaultTestingMouduleForSecureComponent()
具有我的共享模块和其他角度模块。我想使用beforeAll()
而不是beforeEach()
,因为使用beforeEach()
会使我的测试套件变慢。但是,当我使用beforeAll()
时,我的子组件RoureOperationsComponent
无法正确实例化,并且会引发错误。
expect(spy).toHaveBeenCalledWith(...expected)
Expected: true
Number of calls: 0