如何在角度单元测试中覆盖箭头函数?

时间:2021-02-18 09:59:01

标签: node.js angular typescript unit-testing jasmine

this.service = () => { -- statements -- }

以上语句是在angular中使用jasmine单元测试进行测试的。 我能得到一些建议吗?

it("should service call",()=>{ // i want to call the arrow function here like component.service.? what to use in place of '?'. })

1 个答案:

答案 0 :(得分:0)

这是一个函数,所以你可以立即调用它:

示例:

 interface Service {
   fun: () => string;
 }

 class Component {
   constructor() {
     this.service = () => {
       return {
         fun: () => 'hello'
       };
     };
   }
   service: () => Service;
 }

调用:

 const component = new Component();
 const service = component.service();
 const message = service.fun();
 
 // or in one line:
 const message = new Component().service().fun();
 

Typescript playground example