-1
我正在为现有的AngularJS应用程序编写单元测试。这项服务只有四种方法。我能够让FollowUpList工作,但是refresh()不起作用,这是一个非常简单的方法。在Angular JS服务单元测试中未定义的方法
刷新方法应该简单地设置deferredGetFollowUpList = null并在我的测试中返回true。
我得到的错误是:TypeError: Cannot read property 'then' of undefined
,所以我的刷新方法是未定义的。为什么会这样?由于
服务
(function() {
"use strict";
angular
.module("all.patient.details")
.factory("followUpListService", ["$rootScope", "$http", "userContext", "$q", "$uibModal", "htmlBaseUrl", FollowUpListService]);
function FollowUpListService($rootScope, $http, userContext, $q, $uibModal, htmlBaseUrl) {
var deferredGetFollowUpList = null;
return {
getFollowUpList: getFollowUpList,
displayModal: displayModal,
refresh: refresh,
save: save
}
function refresh() {
deferredGetFollowUpList = null;
}
}
})();
单元测试
describe("followUpListService", function() {
beforeEach(module("all.patient.details"));
var followUpListService = {};
var $httpBackend;
var htmlBaseUrlMock;
var returnedFollowUpListData;
var deferredGetFollowUpList;
var $rootScope;
var $q;
var $uibModal;
beforeEach(function() {
htmlBaseUrlMock = { format: function() { } };
module(function ($provide) {
$provide.value("htmlBaseUrl", htmlBaseUrlMock);
});
inject(function (_$rootScope_, _$httpBackend_, _$q_, _$uibModal_, _followUpListService_) {
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
$q = _$q_;
$uibModal = _$uibModal_;
followUpListService = _followUpListService_;
});
});
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("calls refresh()", function() {
followUpListService.refresh()
.then(function (data) {
deferredGetFollowUpList = data;
});
expect(deferredGetFollowUpList).toBe(null);
});
刷新的功能回报'undefined'。未定义不具有属性 - 因此,这是错误的根源。你需要从'refresh()'方法返回promise。 –