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

Jasmine - 如何監(jiān)視實(shí)例方法

Jasmine - how to spyOn instance methods(Jasmine - 如何監(jiān)視實(shí)例方法)
本文介紹了Jasmine - 如何監(jiān)視實(shí)例方法的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號..

我有一個(gè)函數(shù)

var data = {};
var myFunc = function() {
  data.stuff = new ClassName().doA().doB().doC();
};

我想測試一下 doAdoBdoC 都被調(diào)用了.

I'd like to test that doA, doB, and doC were all called.

我嘗試像這樣監(jiān)視實(shí)例方法

I tried spying on the instance methods like this

beforeEach(function() {
  spyOn(ClassName, 'doA');
};
it('should call doA', function() {
  myFunc();
  expect(ClassName.doA).toHaveBeenCalled();
});

但這只會給我一個(gè)doA() 方法不存在"的錯(cuò)誤.

but that just gives me a "doA() method does not exist" error.

有什么想法嗎?

推薦答案

你出錯(cuò)的地方在于你對如何在靜態(tài)上下文中引用 JavaScript 方法的理解.您的代碼實(shí)際上是在監(jiān)視 ClassName.doA(即作為屬性 doA 附加到 ClassName 構(gòu)造函數(shù)的函數(shù),它不是你想要的).

Where you went wrong was your understanding of how to refer to methods in JavaScript in a static context. What your code is actually doing is spying on ClassName.doA (that is, the function attached to the ClassName constructor as the property doA, which is not what you want).

如果您想檢測何時(shí)在任何地方的任何 ClassName 實(shí)例上調(diào)用該方法,則需要監(jiān)視原型.

If you want to detect when that method gets called on any instance of ClassName anywhere, you need to spy on the prototype.

beforeEach(function() {
  spyOn(ClassName.prototype, 'doA');
});
it('should call doA', function() {
  myFunc();
  expect(ClassName.prototype.doA).toHaveBeenCalled();
});

當(dāng)然,這是假設(shè) doA 存在于原型鏈中.如果它是一個(gè)自己的屬性,那么如果不能引用 myFunc 中的匿名對象,就沒有可以使用的技術(shù).如果您可以訪問 myFunc 中的 ClassName 實(shí)例,那將是理想的,因?yàn)槟梢灾苯?spyOn 該對象.

Of course, this is assuming that doA lives in the prototype chain. If it's an own-property, then there is no technique that you can use without being able to refer to the anonymous object in myFunc. If you had access to the ClassName instance inside myFunc, that would be ideal, since you could just spyOn that object directly.

附:你真的應(yīng)該把茉莉花"放在標(biāo)題里.

P.S. You should really put "Jasmine" in the title.

這篇關(guān)于Jasmine - 如何監(jiān)視實(shí)例方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認(rèn)為文檔“準(zhǔn)備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發(fā)技術(shù)
What do jasmine runs and waitsFor actually do?(jasmine 運(yùn)行和等待實(shí)際上是做什么的?)
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 對鏈?zhǔn)椒椒ㄟM(jìn)行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監(jiān)視函數(shù)中的函數(shù)調(diào)用?)
主站蜘蛛池模板: 91夜夜夜| 久久久91精品国产一区二区精品 | 在线免费91| 91精品国模一区二区三区 | 中文字幕一区二区三区精彩视频 | 国产精品一区二区视频 | 亚洲精品一区二区网址 | 欧美日在线| 亚洲一区二区三区在线播放 | 91在线视频播放 | 国产精品久久久久久久免费大片 | 亚洲精品亚洲人成人网 | 亚洲精品久久 | 日韩免费在线视频 | 三级国产三级在线 | 亚洲欧美日韩精品久久亚洲区 | 日韩欧美国产电影 | 成人综合视频在线观看 | 成人免费看片网 | 久久综合久 | 玖玖综合网 | 亚洲国产精品一区二区久久 | 久久久www成人免费无遮挡大片 | 中文字幕11页 | 午夜网| 国产一区999| 欧美精品综合在线 | 日韩中文字幕在线视频 | 国产精品久久视频 | 日韩欧美亚洲 | 久久九九色 | caoporn国产| 毛片一区 | 亚洲成av | 亚洲精品自拍 | 中文字幕日韩欧美一区二区三区 | 国产精品无码专区在线观看 | 日韩欧美中文 | 欧美久久国产精品 | 亚洲第一黄色网 | 亚洲永久在线 |