我正在尝试为依赖于另一个返回promise的工厂函数的服务编写单元测试。我已按照此question和answer中的过程进行了操作,但出于某种原因,我正在尝试验证的then()中的代码未被调用。我如何让它工作?如果可能的话,我想使用没有使用spyOn的版本(答案中的更新部分)。
var app = angular.module('test', []);
app.factory('managerService', (managerModel) => {
var service = {
getCurrentUser: getCurrentUser
}
function getCurrentUser() {
return managerModel.api.get();
}
return service;
})
describe('promise with provider', () => {
var managerModelMOCK = {
api: {}
};
var $q;
var $rootScope;
var deferredUser;
// beforeEach(inject(()=> {})) //no inject here: Injector already created, can not register a module!
beforeEach(angular.mock.module('test'));
beforeEach(() => {
angular.mock.module(function ($provide) {
$provide.value('managerModel', managerModelMOCK); // override version here
});
});
beforeEach(inject(($q) => {
deferredUser = $q.defer();
managerModelMOCK.api.get = () => {
return deferredUser.promise;
}
}));
it('should resolve a promise', angular.mock.inject((managerService, $rootScope) => {
expect(managerService).toBeDefined();
var expected = {
Name: 'Somebody'
};
var actual;
//this doesn't work either
// var innerPromise = $q.defer();
// innerPromise.resolve(expected);
// managerModel.api.get = () => innerPromise.promise;
deferredUser.resolve(expected);
$rootScope.$apply();
managerService.getCurrentUser()
.then((user) => {
actual = user;
});
//undefined, why?
expect(actual).toBeDefined();
}))
})
答案 0 :(得分:0)
呃,保证会出现菜鸟错误。承诺链中的then()
函数需要return
,因此此代码有效:
managerService.getCurrentUser()
.then((user) => {
actual = user;
//promise chains have to return
return;
});
//rootscope.$apply() had to be moved
$rootScope.$apply();
我曾试过将$ rootScope。$ apply()放在承诺链之前,但是当然没有返回值仍然会使它无法正常工作。