創建單元測試這是我的dashboardService,我想創建單元測試的getModulesAngularJs單元測試茉莉花+卡姆拉+角模擬了getModules功能
============== ================================================== ================
(function (angular) {
'use strict';
var __Name = "dashboardService";
var __Path = "components/dashboard";
angular
.module(window.__env.appName,[])
.factory(__Name, serviceFunc);
serviceFunc.$inject = ['$cookies','$http'];
function serviceFunc($cookies,$http) {
return {
getmodules: function (cb) {
$http({
method: 'GET',
url:'/home/modulesInfo'
}).then(function successCallback(response) {
cb(true, response);
}, function errorCallback(response) {
cb(false, response)
});
}
}
}
})(window.angular);
=========================== =========================================
以下是我的說明文件(dashboardService.spec.js)
'use strict';
describe('getmodules', function() {
beforeEach(function() { module('ngMockE2E'); });
var service, httpBackend, defaultAlertFactory;
beforeEach(function() {
debugger;
module('myApp');
module(function ($provide) {
var dashboardService = {
getMegetmodules: function() {
// mocked method
return ['david', 'James', 'Sam'];
}
};
$provide.service('defaultAlertFactoryA', dashboardService);
});
angular.mock.inject(function ($injector) {
service = $injector.get('defaultAlertFactoryA');
httpBackend = $injector.get('$httpBackend');
});
});
//describe('getmodules', function() {
it("should return a list of getmodules", inject(function() {
debugger;
httpBackend.expectGET('/Home/modulesInfo').respond(['david', 'James', 'Sam']);
service.getmodules(function (result) {
expect(result).toEqual(["david", "James", "Sam"]);
});
httpBackend.flush();
}))
//})
});
謝謝!
這裏是錯誤信息::::::::: getmodules應該返回一個getmodules列表 錯誤:[ng:areq] Argument 'fn'不是函數,得到對象 http://errors.angularjs.org/1.6.1/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20Object 錯誤:[ng: areq]參數'fn'不是函數,得到對象 – user1308116