1
我正在測試角度服務,我已經在打字稿中編寫代碼,但是當我運行測試用例時,出現「this.someFunction」錯誤。 show()「不是函數,並且」this.someFunction.show()「在我的實際服務中。 這是我想要做的。在運行與茉莉花angular2單元測試時調用服務時出錯
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(); //work fine
methodTwo(false); //work fine
methodThree(true); //work fine
methodFour(); //work fine
methodFive(1); //work fine
methodSix(); //work 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);
}
這裏是我的現金付款handler.service.ts文件
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); //getting error on this line
}
}
和代碼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
}
我在許多論壇上搜索過解決方案,花了一整天的時間,但無法解決。任何幫助將不勝感激。 Meherbani hosi nay。
如果錯誤在服務中,則不在.spec上,那麼不是測試錯誤,而是服務錯誤,對嗎?或者,也許我不太明白? – SrAxi