繁体   English   中英

如何在 angular 的单元测试中覆盖箭头功能?

[英]How to cover arrow functions in unit testing in angular?

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 '?'. })

它是一个 function 所以你立即调用它:

例子:

 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 操场示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM