我无法获得一个模拟方法,该方法返回一个承诺,触发当时连接到它。
这是正在测试的服务。 get方法是它的重要方面。
angular.module('service.overtimeRules').factory('OvertimeRules', OvertimeRules);
function OvertimeRules(_, Message, OvertimeRulesModel) {
const rules = [];
return { get };
function get() {
return OvertimeRulesModel.get().then(
data => {
console.log('This never fires :(');
rules.push(...data);
},
() => Message.add('Error retrieving the overtime rules.', 'danger')
);
}
}
这是spec文件。我得到的错误是TypeError: undefined is not an object (evaluating 'OvertimeRulesModel.get')
。如果我删除了get describe包装器,则错误消失,但服务方法的then
块仍然不会触发。
describe('OvertimeRules', () => {
let $q, $rootScope, deferred, Message, OvertimeRules, OvertimeRulesModel;
beforeEach(() => {
module('globals');
module('service.overtimeRules');
module($provide => {
$provide.value('Message', Message);
$provide.value('OvertimeRulesModel', OvertimeRulesModel);
});
inject((_$q_, _$rootScope_, _OvertimeRules_) => {
$q = _$q_;
$rootScope = _$rootScope_;
OvertimeRules = _OvertimeRules_;
deferred = { OvertimeRulesModel: { get: $q.defer(), update: $q.defer } };
Message = jasmine.createSpyObj('Message', ['add']);
OvertimeRulesModel = jasmine.createSpyObj('OvertimeRulesModel', ['get']);
OvertimeRulesModel.get.and.returnValue(deferred.OvertimeRulesModel.get.promise);
});
});
describe('get', () => { // why would this cause an error?
it('gets a list of rules and pushes them into the rules array', () => {
deferred.OvertimeRulesModel.get.resolve(['rules']);
OvertimeRules.get();
$rootScope.$digest(); // $apply doesn't work either
expect(OvertimeRules.rules).toEqual(['rules']);
});
});
});
可能重要的说明:
OvertimeRulesModel
是此service.overtimeRules
模块中的另一个工厂; Message
是单独模块的一部分。OvertimeRulesModel
移动到自己的模块,但遇到了同样的错误。A = jasmine.createSpyObj(...); A.get.and.returnValue(promise);
控制器规范中的方法,一切正常。OvertimeRulesModel
服务。答案 0 :(得分:0)
似乎我正在丢失提供给模块提供者的变量的引用。解决方案只是将createSpyObj
调用移到$provide
语句之上。
...
module('service.overtimeRules');
Message = jasmine.createSpyObj('Message', ['add']);
OvertimeRulesModel = jasmine.createSpyObj('OvertimeRulesModel', ['get']);
module($provide => {
$provide.value('Message', Message);
$provide.value('OvertimeRulesModel', OvertimeRulesModel);
});
...
使用解决方案修复了我遇到的另一个问题。此外,我不必更改returnValue
语法。
显然,我需要做的就是将returnValue
更改为callFake
,如下所示:
OvertimeRulesModel = jasmine.createSpyObj('OvertimeRulesModel', ['get']);
OvertimeRulesModel.get.and.callFake(() => deferred.OvertimeRulesModel.get.promise);