【问题标题】:How to cover arrow functions in unit testing in angular?如何在角度单元测试中覆盖箭头函数?
【发布时间】:2021-05-21 06:18:43
【问题描述】:

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

【问题讨论】:

  • component.service()
  • 不工作选项可用:调用、绑定、应用......
  • 应该可以工作,请注意删除的点 => .。不是component.service.(),只是component.service()
  • this.service.fun = () => {} 现在我想打电话给乐趣
  • 现在它通过错误fun is not a function

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


【解决方案1】:

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

示例:

 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

【讨论】:

  • 谢谢,我有问题了。
猜你喜欢
  • 1970-01-01
  • 2019-11-01
  • 2021-03-29
  • 2019-04-21
  • 2016-09-04
  • 2020-05-12
  • 2020-06-05
  • 2019-05-24
  • 2019-08-04
相关资源
最近更新 更多