如何使用 $provide 和 $q 对工厂进行单元测试
How do you use unit test a factory using $provide and $q
我正在尝试为依赖于 returns 承诺的另一个工厂函数的服务编写单元测试。我已按照此 question and 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();
}))
})
呃,承诺菜鸟错误。承诺链中的 then()
函数需要 return
,因此此代码有效:
managerService.getCurrentUser()
.then((user) => {
actual = user;
//promise chains have to return
return;
});
//rootscope.$apply() had to be moved
$rootScope.$apply();
我之前曾尝试将 `$rootScope.$apply() 放在 promise 链之后,但当然没有 return 仍然无法正常工作。
我正在尝试为依赖于 returns 承诺的另一个工厂函数的服务编写单元测试。我已按照此 question and 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();
}))
})
呃,承诺菜鸟错误。承诺链中的 then()
函数需要 return
,因此此代码有效:
managerService.getCurrentUser()
.then((user) => {
actual = user;
//promise chains have to return
return;
});
//rootscope.$apply() had to be moved
$rootScope.$apply();
我之前曾尝试将 `$rootScope.$apply() 放在 promise 链之后,但当然没有 return 仍然无法正常工作。