这是我的dashboardService,我想为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);
=============================================== =====================
'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();
}))
//})
谢谢!
答案 0 :(得分:0)
看起来你有一个拼写错误,你的dashboardService定义了一个名为getMegetmodules的函数,在你的测试函数中你调用了service.getmodules,你应该这样做:
module(function ($provide) {
var dashboardService = {
getModules: function () {
// mocked method
return ['david', 'James', 'Sam'];
}
};
$provide.service('defaultAlertFactoryA', dashboardService);
});
在你的测试功能中:
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();
}));
希望有所帮助!
pd:我认为你不需要那个httpBackend.flush()