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

如何在另一個方法中創建的對象上使用 Jasmine 間

How to use Jasmine spies on an object created inside another method?(如何在另一個方法中創建的對象上使用 Jasmine 間諜?)
本文介紹了如何在另一個方法中創建的對象上使用 Jasmine 間諜?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

鑒于以下代碼片段,您將如何創建 Jasmine spyOn 測試以確認 doSomething 在您運行 MyFunction 時被調用?

Given the following code snippet, how would you create a Jasmine spyOn test to confirm that doSomething gets called when you run MyFunction?

function MyFunction() {
    var foo = new MyCoolObject();
    foo.doSomething();
};

這是我的測試的樣子.不幸的是,在評估 spyOn 調用時出現錯誤:

Here's what my test looks like. Unfortunately, I get an error when the spyOn call is evaluated:

describe("MyFunction", function () {
    it("calls doSomething", function () {

        spyOn(MyCoolObject, "doSomething");
        MyFunction();
        expect(MyCoolObject.doSomething).toHaveBeenCalled();

    });
});

Jasmine 此時似乎無法識別 doSomething 方法.有什么建議嗎?

Jasmine doesn't appear to recognize the doSomething method at that point. Any suggestions?

推薦答案

當您調用 new MyCoolObject() 時,您會調用 MyCoolObject 函數并使用相關原型.這意味著當您 spyOn(MyCoolObject, "doSomething") 時,您不是在 new 調用返回的對象上設置間諜,而是在可能的 MyCoolObject 函數本身的>doSomething 函數.

When you call new MyCoolObject() you invoke the MyCoolObject function and get a new object with the related prototype. This means that when you spyOn(MyCoolObject, "doSomething") you're not setting up a spy on the object returned by the new call, but on a possible doSomething function on the MyCoolObject function itself.

您應該能夠執行以下操作:

You should be able to do something like:

it("calls doSomething", function() {
  var originalConstructor = MyCoolObject,
      spiedObj;
  spyOn(window, 'MyCoolObject').and.callFake(function() {
    spiedObj = new originalConstructor();
    spyOn(spiedObj, 'doSomething');
    return spiedObj;
  });
  MyFunction();
  expect(spiedObj.doSomething).toHaveBeenCalled();
});

這篇關于如何在另一個方法中創建的對象上使用 Jasmine 間諜?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 色婷婷久久久久swag精品 | 午夜欧美一区二区三区在线播放 | 日韩淫片免费看 | 亚洲国产精品久久久久秋霞不卡 | 亚洲毛片 | 久久激情视频 | 亚洲精品久久久久久久久久久久久 | 中文字幕国产精品视频 | 国产99视频精品免费播放照片 | 成人在线一级片 | 国产精品久久久久久 | 九九热热九九 | jlzzjlzz欧美大全| 懂色av一区二区三区在线播放 | 亚洲一区二区三区在线播放 | 成人免费观看男女羞羞视频 | 美国黄色毛片 | 天天躁日日躁狠狠的躁天龙影院 | 人人干在线视频 | 91久久国产综合久久91精品网站 | 久久国产激情视频 | 91大神在线资源观看无广告 | 韩国久久| 久久久国产精品 | 青青草综合| 亚洲精品久久嫩草网站秘色 | 成人av一区二区亚洲精 | 国产永久免费 | 99久久免费精品视频 | 国产视频一区二区三区四区五区 | 国产在线精品一区二区 | 久久影音先锋 | 涩色视频在线观看 | 婷婷色婷婷 | 麻豆精品国产免费 | 亚洲美乳中文字幕 | 超碰国产在线 | 在线观看视频亚洲 | 国产欧美一区二区精品忘忧草 | 亚洲a在线视频 | 不卡一区|