0
這成功地測試這個功能是在控制器中的功能:
var vm = this;
vm.getData = getData;
function getData(val) {
return $http.get('/get-data', {
params: {
query: val
}
}).then(function(response) {
return response.data;
});
}
,這是我(精簡)的測試文件:
describe('Controller: MyCtrl', function() {
'use strict';
var MyCtrl;
var rootScope;
var scope;
var httpMock;
beforeEach(function() {
module('MyModule');
inject(function($controller, $rootScope, $httpBackend) {
rootScope = $rootScope;
scope = $rootScope.$new();
httpMock = $httpBackend;
MyCtrl = $controller('MyCtrl as vm', {
$rootScope: rootScope,
$scope: scope,
$http: httpMock,
});
});
});
describe('vm.getData()', function() {
it('returns the required data', function() {
httpMock.when('GET', '/get-data?query=test-val').respond(200, {data: 'test-data'});
httpMock.flush();
expect(scope.vm.getData('test-val')).toEqual('test-data');
});
});
});
我想測試調用getData()的結果是否返回正確的數據。
目前我收到錯誤$http.get is not a function
。在我的函數中設置一個斷點顯示http被stubbed爲$ httpBackend。
我認爲有一些基本的東西我沒有抓住 - 任何指針將不勝感激。
這是真棒 - 非常感謝你jlast :) – Jay
只是爲了完整性,這裏是一對夫婦的必要IT塊修訂:'它('返回所需數據',function(){var result = {}; httpMock.when('GET','/ get-data ?query=test-val').respond(200,{data :'test-data'}); MyCtrl.getData('test-val')。then(function(_result _){ result = _result_; }); httpMock.flush(); expect(result).toEqual({data:'test-data'}); });' – Jay