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

我什么時候應(yīng)該在我的 Angular JS 單元測試中使用

When should I use $provide versus Jasmine Spies in my Angular JS Unit tests(我什么時候應(yīng)該在我的 Angular JS 單元測試中使用 $provide 和 Jasmine Spies)
本文介紹了我什么時候應(yīng)該在我的 Angular JS 單元測試中使用 $provide 和 Jasmine Spies的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我在一個大型 Angular 應(yīng)用程序上工作,最初我們通過使用 $provide 來模擬服務(wù)進行了很多測試.但是,我們現(xiàn)在在測試中使用了很多 Jasmine Spies,以便對服務(wù)進行存根和監(jiān)視.

I work on a large Angular App and initially we done a lot of our tests by using $provide to mock services. However we now have a lot of Jasmine Spies in our tests in order to stub and spy on services.

spyOn(myService, 'myMethod').andReturn 'myValue'

我們真的應(yīng)該為此使用 $provide,還是在某些情況下監(jiān)視服務(wù)是最好的方法?

Should we really be using $provide for this or are there cases where spying on a service is the best approach?

在 Angular 測試中,他們使用間諜來監(jiān)視 Jquery 我將其視為外部服務(wù).

In the Angular Tests they use spies for spying on Jquery which I would see as an external service.

spyOn(jq.prototype, 'on');

$provide 似乎更多地用于內(nèi)部服務(wù).

$provide seems to be used more for internal services.

  module(function($provide){
    $provide.provider('$exceptionHandler', $ExceptionHandlerProvider);
  });

還有一個 Jasmine createSpy 函數(shù),但現(xiàn)在我認為 $provide 應(yīng)該始終優(yōu)先于它.

There is also a Jasmine createSpy function but now I'm thinking that $provide should always take precedence over that.

對此的任何見解或幫助將不勝感激.

Any insights or help in this would be appreciated.

推薦答案

根據(jù)我自己的(有限的)經(jīng)驗,我會說做任何方法:

From my own (limited) experience, I would say do whatever approach makes:

  • 測試代碼更簡單/更清晰/更短
  • 限制關(guān)于您的測試在內(nèi)部執(zhí)行的代碼的假設(shè).
  • 減少其副作用(例如運行實際的 Ajax 請求)
  • 在條款或運行時間方面盡可能縮短測試時間.

通常 spyOn 方法會起作用,為了完成上述操作,我想從服務(wù)/工廠中存根單個方法.如果我需要模擬整個服務(wù)/工廠,請使用 $provide.

Usually the spyOn approach works when, in order to do the above, I would like to stub a single method from a service / factory. If I need to mock an entire service / factory, then use $provide.

我想到了一些需要其中之一的特定情況:

A few specific cases come to mind that require one or the other:

  • 如果您正在測試一項服務(wù),然后要從該服務(wù)中存根其他方法,您必須使用 spyOn

為了確保稍后不會在被測代碼中引入額外的依賴項,$provide 增加了一些保護.說,如果你想確保 ServiceA 只需要 ServiceB 中的 myMethod,那么 $provide 我認為會是走的路,好像 ServiceA 在測試期間從 ServiceB 調(diào)用任何未定義的方法,都會引發(fā)錯誤.

To ensure that extra dependencies aren't introduced later in the code under test, than $provide adds a bit more protection. Say, if you want to ensure that ServiceA only requires myMethod from ServiceB, then $provide I think would be the way to go, as if ServiceA calls any undefined methods from ServiceB during the test, errors would be raised.

$provide.provider('ServiceB', {
    myMethod: function() {}
});

  • 如果你想模擬一個返回函數(shù)的工廠,那么:

  • If you want to mock a factory that returns a function, so:

    app.factory('myFactory', function() {
      return function(option) {
        // Do something here
      }
    });
    

    用作:

    myFactory(option);
    

    然后驗證某些代碼調(diào)用 myFactory(option) 我認為沒有其他選擇然后使用 $provide 來模擬工廠.

    Then to verify that some code calls myFactory(option) I think there is no alternative then to use $provide to mock the factory.

    順便說一句,它們不是相互排斥的選項.您可以使用 $provide ,然后仍然涉及間諜.在前面的示例中,如果您想驗證工廠是否使用選項調(diào)用,您可能必須:

    Just by the way, they're not mutually-exclusive options. You can use $provide and then still have spies involved. In the previous example, if you want to verify the factory was called with an option, you might have to:

    var myFactorySpy = jasmine.createSpy();
    $provide.provider('myFactory', myFactorySpy);
    

    然后在適當(dāng)?shù)狞c進行測試:

    And then in the test at the appropriate point:

    expect(myFactorySpy).toHaveBeenCalledWith(option);
    

    這篇關(guān)于我什么時候應(yīng)該在我的 Angular JS 單元測試中使用 $provide 和 Jasmine Spies的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 認為文檔“準(zhǔn)備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發(fā)技術(shù)
    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 對鏈?zhǔn)椒椒ㄟM行單元測試)
    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就要激情 | 精品久久久久久亚洲综合网 | 欧美日韩精选 | 久久精品国产免费看久久精品 | 亚洲第一av | 精品欧美一区免费观看α√ | 99精品久久 | 一区二区三区在线 | 日韩欧美三区 | 一区中文| 欧美日韩亚洲二区 | av网站免费在线观看 | av在线成人 | 91美女在线观看 | 国产一区二区欧美 | 国产精品中文字幕一区二区三区 | 欧美日韩精品在线一区 | 亚洲午夜精品一区二区三区他趣 | 亚洲小视频在线播放 | 亚洲激情网站 | 911网站大全在线观看 | 亚洲国产成人精品一区二区 | 午夜影院在线视频 | 日韩精品1区2区3区 国产精品国产成人国产三级 | 日韩一区二区三区视频 | 欧美视频二区 | 羞羞色在线观看 | 精品成人av| 人干人操| 亚洲精品一区在线观看 | 色婷婷综合网 | 精品欧美乱码久久久久久1区2区 | 亚洲成人www| 国产成人aⅴ | 97人人草| 国产极品粉嫩美女呻吟在线看人 | 久久免费观看一级毛片 | 久久久精品 | 丝袜美腿一区二区三区动态图 | 欧美一区免费 |