我正在尝试为我的角度组件编写单元测试。 我能够使用 spyon 成功模拟 2 种方法。 但是即使在我模拟并使用 callFake 之后,一种方法 (_paymentStateService.getMakePaymentState$) 也会出错。 它在这些行中给出错误(this._payStateService.getMakePaymentState$.subscribe is not a function): spyOn(mockMakePaymentState, 'getMakePaymentState$')
fdescribe('DetailsComponent', () => {
let component: DetailsComponent;
let fixture: ComponentFixture<DetailsComponent>;
let httpMock: HttpTestingController;
let formModule: FormsModule;
let form = new FormGroup({
username: new FormControl('', Validators.required),
dob: new FormControl('')
});
let mockbillingService = {
getDetails: () => {},
getBankDetails: () => {}
}
let mockMakePaymentState = {
getPaymentDetails$: () => of({} as IPaymentDetails),
getDetails: () => {},
getUserInfo: () => {}
};
class MatDialogMock {
// When the component calls this.dialog.open(...) we'll return an object
// with an afterClosed method that allows to subscribe to the dialog result observable.
open() {
return {
afterClosed: () => of({action: true})
};
}
}
let paymentInfoDetails = <PaymentInfoDetails>{};
let paymentBankDetails = <PaymentBankDetails>{};
let makePaymentState = <IPaymentDetails> {}
paymentInfoDetails.banks = [paymentBankDetails];
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ],
imports: [ FormsModule, ReactiveFormsModule],
providers: [
{ provide: BillingService, useValue: mockbillingService },
{provide: PayState, useValue: mockMakePaymentState},
{ provide: MatDialog, useClass: MatDialogMock }
]
})
.compileComponents();
fixture = TestBed.createComponent(DetailsComponent);
component = fixture.componentInstance;
httpMock = TestBed.inject(HttpTestingController);
formModule = TestBed.inject(FormsModule);
// fixture.detectChanges();
});
it('should call ngOnInit', () => {
spyOn(mockMakePaymentState, 'getDetails').and.callFake(() => {
return of(
paymentInfoDetails
);
});
spyOn(mockMakePaymentState, 'getBankPayments').and.callFake(() => {
return of(
paymentBankDetails
);
});
spyOn(mockMakePaymentState, 'getMakePaymentState$').and.callFake(() => {
return of(makePaymentState);
})
fixture.detectChanges();
});
});