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

您如何模擬作為函數(shù)的 Angular 服務(wù)?

How do you mock Angular service that is a function?(您如何模擬作為函數(shù)的 Angular 服務(wù)?)
本文介紹了您如何模擬作為函數(shù)的 Angular 服務(wù)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我們有一個我們稱之為 CORShttpService 的東西,它基本上是 $http 服務(wù)的包裝器,但封裝了一些我們需要的 CORS 功能.我現(xiàn)在正在為注入了 CORShttpService 的服務(wù)編寫一些測試.該服務(wù)的代碼如下:

We have a what we call a CORShttpService, which is basically a wrapper aroung the $http service, but encapsulates some CORS functionality that we need. I'm now writing some tests for a service that has the CORShttpService injected into it. This service has code like this:

CORShttpService({method: requestMethod, url: getUrl(path), data: data}).
    success(function(data, status, headers) {
        //do success stuff
    }).
    error(function(data, status, headers) {
       //do error stuff
    });

我想模擬對 CORShttpService 的調(diào)用,但我不知道該怎么做.我正在使用 Jasmine,它的 spyOn 函數(shù)需要一個對象來模擬對象上的函數(shù).我的 CORShttpService 沒有附加到任何對象,所以我不知道如何去模擬它.是的,我可以使用 $httpBackend 來模擬最終在 CORShttpService 中設(shè)置的請求,但我不希望它首先進入該服務(wù).我想隔離單元測試并簡單地模擬外部調(diào)用.有什么方法可以模擬這個只是一個函數(shù)的服務(wù)嗎?

I want to mock the call to CORShttpService, but I'm not sure how to go about doing it. I'm using Jasmine, and its spyOn function requires an object to mock the function on the object. My CORShttpService isn't attached to any object, so I don't know how to go about mocking it. Yes, I could use $httpBackend to mock the requests that eventually get set in the CORShttpService, but I don't want it going into that service in the first place. I want to isolate the unit test and simply mock the external calls. Is there any way I can mock this service that is just a function?

推薦答案

我想多了,$httpBackend 服務(wù)確實提供了很多測試請求的功能.由于我的 CORShttpService 基本上是一個圍繞 $http 的包裝器,因此我決定如果我對 CORShttpService 進行模擬實現(xiàn),我可能會物有所值 簡單的 $http 實現(xiàn).使用 本文檔 來幫助我,我的規(guī)范中有以下內(nèi)容:

As I thought about this more, the $httpBackend service does provide a lot of functionality for testing requests. As my CORShttpService is a basically a wrapper around $http, I decided I could probably get the most bang for my buck, if I made the mock implementation of CORShttpService simple the $http implementation. Using this documentation to help me, I have the following in my spec:

beforeEach(module(function($provide) {
    $provide.provider('CORShttpService', function() {
        this.$get = function($http) {
            return $http;
        };
    });
}));

所以,我的任何想要注入 CORShttpService 的服務(wù)現(xiàn)在基本上只注入了 $http,因此我可以使用所有的 $httpBackend 功能,無需擔心 CORShttpService 本身的額外功能.

So, any of my services wanting the CORShttpService injected, will now basically just have $http injected, and thus allow me to use all the $httpBackend functionality without the concern of the extra functionality found in the CORShttpService itself.

這適用于我的具體情況,但就模擬只是一個函數(shù)的服務(wù)的一般解決方案而言,同樣的事情也可以用 zbynour 中提到的 jasmine.createSpy 來完成回答.比如:

This works for my specific case, but as far as a general solution for mocking services that are just a function, the same kind of thing could probably be done with jasmine.createSpy as mentioned in zbynour's answer. Something like:

beforeEach(module(function($provide) {
    $provide.provider('MyService', function() {
        this.$get = function() {
            return jasmine.createSpy("myService");
        };
    });
}));

這篇關(guān)于您如何模擬作為函數(shù)的 Angular 服務(wù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 認為文檔“準備好之前,如何讓我的 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 對鏈式方法進行單元測試)
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)用?)
主站蜘蛛池模板: 久久久久中文字幕 | 国产在线精品免费 | 天天干天天爱天天 | 欧美啪啪网站 | 鸳鸯谱在线观看高清 | 美女国内精品自产拍在线播放 | 天堂资源视频 | 四虎影音| 一级做a爰片久久毛片免费看 | 人人爽人人爽 | 91精品中文字幕一区二区三区 | 久久久做| 国产一区二区三区欧美 | 在线亚洲免费视频 | 伊人免费在线 | 欧洲精品久久久久毛片完整版 | 亚洲免费视频播放 | 欧美日韩黄色一级片 | 欧美午夜在线 | a毛片| 国产精品国产三级国产aⅴ中文 | 不卡的av在线 | 中文在线一区二区 | 久久精品国产久精国产 | 亚洲少妇综合网 | 在线欧美 | 国产成人精品在线播放 | 国产永久免费 | 最新91在线| 午夜影院普通用户体验区 | 日韩av大片免费看 | 欧美精品中文字幕久久二区 | 欧区一欧区二欧区三免费 | 欧美在线| 黄色日批视频 | 国产精品一区二区在线观看 | 午夜影院在线观看免费 | 国产日韩欧美中文字幕 | 中文字幕亚洲一区二区三区 | 一区二区精品 | 日韩在线播放网址 |