久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SpyOn 單獨導出的 ES6 函數

SpyOn individually exported ES6 functions(SpyOn 單獨導出的 ES6 函數)
本文介紹了SpyOn 單獨導出的 ES6 函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

tl;博士:

  1. 我用茉莉花
  2. 我想測試調用 bbbaaa 函數模塊;
  3. 我想監視 bbb,但最終 aaa 調用了原bbb函數,不是spy;
  1. I use Jasmine;
  2. I want to test aaa function which called bbb from the same module;
  3. I want to spy on bbb, but eventually aaa called the original bbb function, not a spy;

如何強制 aaa 調用間諜?

How can I force aaa to call the spy?

模塊:

export function aaa() {
  return bbb();
}

export function bbb() {
  return 222;
}

測試:

import * as util from 'my-module';

describe('aaa test', () => {

  let bbbSpy: Spy;

  beforeEach(() => {
    bbbSpy = spyOn(util, 'bbb');
  });

  it('should return SPYED', () => {
    bbbSpy.and.returnValue('SPYED!!');
    const result = util.aaa();
    expect(result).toEqual('SPYED!!'); // Doesn't work - still 222
  });

});

所以,這基本上是行不通的.誰能幫幫我?

So, basically that doesn't work. Can anyone help me please?

P.S.我不想更改模塊的代碼,因為在這種情況下我將不得不更改項目中的大量代碼.我需要一個通用的測試解決方案.

P.S. I don't want to change the module's code, because in that case I'll have to change tons of code in the project. I need a general solution for tests.

推薦答案

即使認為不是同一個框架,有一個相關的問題告訴你為什么這不起作用:如何使用 jest 在同一模塊中模擬函數.基本上,您將無法訪問該函數的固定引用,而是明確表示該函數與模塊上下文中的函數相同.

Even thought is not the same framework, there is a related question that gives you why this doesnt work: How to mock functions in the same module using jest. Basically you wont be able to access this fixed reference of the function and instead make clear that is the same function as in the context of the module.

我知道這不滿足您在問題中發布的約束,但很明顯,您想要實現的目標是無法通過使用 spy.

I know this doesn't satisfy the constraint that you post in your question, but it just makes obvious that what you want to achieve is not possible by using spy.

使用 JavaScript,沒有辦法交換對某些東西的引用.您不能換出模塊內部的函數.當您嘗試在示例中使用 spyOn.and.returnValue 覆蓋 bbb 時,您只是在修改測試中的本地綁定 bbb,但它對您其他文件中的 bbb 綁定沒有影響.

With JavaScript, there is no way to swap out references to something. You cannot swap out a function that is internal to a module. When you try to overwrite bbb with spyOn.and.returnValue in your example, you are just modifying the local binding bbb in your test but it has no effect on the bbb binding in your other file.

這篇關于SpyOn 單獨導出的 ES6 函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 99色在线| 久久久久久国产精品免费免费 | 91精品国产一区二区三区蜜臀 | 北条麻妃一区二区三区在线视频 | 一区中文字幕 | 亚洲精品国产电影 | 午夜国产精品视频 | 毛片站 | 欧美综合久久 | 精品国产一区二区三区性色av | 99精品一区二区 | 手机av网| 精品亚洲一区二区三区四区五区 | 欧美一级精品片在线看 | 日韩欧美一区二区三区 | 免费精品视频在线观看 | 精品视频一区二区三区在线观看 | 久操亚洲 | 国产97视频在线观看 | 国产乱码精品一区二区三区中文 | 亚洲国产精品久久久 | 久久1区| 高清国产一区二区 | 日韩免费一二三区 | 天天躁日日躁狠狠很躁 | 午夜视频在线播放 | www.887色视频免费 | 免费在线一区二区 | 成人精品久久 | 国产精品视频播放 | 99久久久国产精品免费消防器 | 国产精品jizz在线观看老狼 | 成人免费小视频 | 天堂素人约啪 | 香蕉久久久久久 | 伊人久久综合 | 欧美jizzhd精品欧美巨大免费 | 久在线 | 欧美狠狠操 | 欧美日韩在线精品 | 蜜桃视频在线观看免费视频网站www |