Struggling with testing an angular service, my TypeScript code seems correct but I keep getting an error in my test cases for "this.someFunction.show()" not being a function, even though it exists in my actual service. Here is what I'm attempting to do:
myService.spec.ts
import { dep1, dep......m dep-n } from '../../../../some-modules';
describe('ServiceTest', () => {
let cashPaymentHandlerService : CashPaymentHandlerService;
it('for cash, when pick list enabled, 1 cash drawer, adds payment line', () => {
methodOne(); //works fine
methodTwo(false); //works fine
methodThree(true); //works fine
methodFour(); //works fine
methodFive(1); //works fine
methodSix(); //works fine
givenDependanciesAreMocked();
whenHandlePaymentIsInvoked();
expect(cashDrawerService.openCashDrawer).toHaveBeenCalled();
});
function givenDependanciesAreMocked(){
TestBed.configureTestingModule({
providers: [
{provide: ServiceOne, useValue: serviceOne},
{provide: ServiceTwo, useValue: serviceTwo},
{provide: ServiceN, useValue: serviceN},
CashPaymentHandlerService
]
});
cashPaymentHandlerService = TestBed.get(CashPaymentHandlerService);
}
function whenHandlePaymentIsInvoked() : any {
cashPaymentHandlerService.handlePayment(cashPaymentRequest);
}
My cash-payment-handler.service.ts file looks like this:
import { dep1, dep......m dep-n } from '../../../../some-modules';
export class CashPaymentHandlerService {
constructor(private service1 : Service1,
private service2: Service2,
private serviceN: ServiceN){
}
handlePayment(cashPaymentRequest: CashPaymentRequest) {
let request = new service1();
request.initialAmount = this.service2.dataModel.transactionDataModel.balance();
request.onValueEntered = (response: AmountPromptResponse) => {
if (!response.cancelled){
this.addCashPaymentLine(response.value, cashPaymentRequest); // runs fine
}
};
this.serviceN.show(request); //error occurs at this line
}
}
And here's the relevant code snippet from ServiceN.ts:
constructor(
private someController: SomeController){
}
show(options?: NewRequest): void {
if (!options){
options = new NewRequest();
}
this.someController.show(PromptComponent, options); // PromptComponent imported from some other component
}
I've searched various forums and spent a whole day on this issue without resolution. Any assistance would be greatly appreciated.