In my component, I have a subscription to triggerRuleExecutionService
which is triggered by another component using next()
.
Within the pipe, I am using switchMap
to make an HTTP service call and retrieve data from the database.
this.ruleExecutionService = this.editCheckSVC.triggerRuleExecutionService.pipe(
switchMap(res => {
return this.editCheckSVC.executeRules(res);
})
).subscribe(res => {
console.log(res);
});
This code snippet resides within the ngOnInit
function.
Below is the test specification for the above functionality.
const ruleExecutionSubject = new Subject();
class EditChkManagementServiceStub {
triggerRuleExecutionService = ruleExecutionSubject.asObservable();
executeRules() {
return of([])
}
}
describe('EditcheckManagmentComponent', () => {
let component: EditcheckManagmentComponent;
let fixture: ComponentFixture<EditcheckManagmentComponent>;
let debugElement: DebugElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [EditcheckManagmentComponent],
schemas: [NO_ERRORS_SCHEMA],
providers: [{ provide: EditCheckManagementService, useClass: EditChkManagementServiceStub }, HttpService],
imports: [HttpClientModule]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(EditcheckManagmentComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
fixture.detectChanges();
});
it('should call rule execution API', () => {
ruleExecutionSubject.next({
formName: '',
schedule: '',
subSchedule: '',
version: '',
fiscalYear: '2018',
accountingPeriod: '6'
});
fixture.detectChanges();
fixture.whenStable().then(() => {
const executionServiceInstance: EditCheckManagementService = TestBed.get(EditCheckManagementService);
spyOn(executionServiceInstance, 'executeRules').and.callThrough();
component.ngOnInit()
expect(executionServiceInstance.executeRules).toHaveBeenCalled();
});
});
});
The test case is failing with the message
Expected spy executeRules to have been called.
Can you identify what mistake may have been made here?