問題描述
我們有一個我們稱之為 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)!