简体   繁体   English

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

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

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

The above statement is to be tested using jasmine unit testing in angular.上面的语句要使用 angular 中的 jasmine 单元测试进行测试。 Can i get some suggestions for that?我能得到一些建议吗?

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

It is a function so you call it immediately:它是一个 function 所以你立即调用它:

example:例子:

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

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

Calling it:调用它:

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

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

相关问题 模块中的单元测试功能 - unit-testing functions in module 如何使用 jest 访问导出函数的对象以进行单元测试 - How to access objects of exported functions for unit testing using jest 如何将依赖项注入 Firebase/Google Cloud Functions? (单元和集成测试) - How to inject dependencies into Firebase/Google Cloud Functions? (unit & integration testing) 如何从快速路由获取功能以进行单元测试? - How to get functions from express routes for unit testing? 如何使用 Chai 和 Mocha 进行 Google Cloud 功能单元测试 - How to use Chai and Mocha for Google Cloud functions unit testing 如何为箭头函数解析此绑定? - How is this binding resolved for arrow functions? 在对单元测试显示模块时如何存根私有函数 - How do you stub private functions when unit testing a revealing module 如何设置用于单元测试的测试套件 Firebase 与模拟器连接的功能,特别是 Firestore 模拟器? - How to setup a test suit for unit testing Firebase Functions that connect with emulators, specifically the Firestore emulator? 如何在 nodejs 中依赖外部变量的笑话中覆盖“if 语句”的单元测试 - How to cover unit test of "if statement" in jest dependeing of external variable in nodejs 如何在es6类中编写箭头函数? - How to write arrow functions in es6 class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM