Angularjs 错误:超时-未在指定的超时内调用异步回调。默认超时时间间隔
我有一个角度服务类:-Angularjs 错误:超时-未在指定的超时内调用异步回调。默认超时时间间隔,angularjs,unit-testing,asynchronous,jasmine,Angularjs,Unit Testing,Asynchronous,Jasmine,我有一个角度服务类:- angular.module('triggerTips') .service('userData', function ($rootScope, $http, $log, $firebase) { this._log = { service : 'userData' }; // Synchronized objects storing the user data var config; var us
angular.module('triggerTips')
.service('userData', function ($rootScope, $http, $log, $firebase) {
this._log = {
service : 'userData'
};
// Synchronized objects storing the user data
var config;
var userState;
// Loads the user data from firebase
this.init = function(readyCallback) {
var log = angular.extend({}, this._log);
log.funct = 'init';
var fireRef = new Firebase('https://XYZfirebaseio.com/' + $rootScope.clientName);
config = $firebase(fireRef.child('config')).$asObject();
userState = $firebase(fireRef.child('userState').child($rootScope.userName)).$asObject();
Promise.all([config.$loaded(), userState.$loaded()]).
then(
function() {
if(config == null || Object.keys(config).length < 4) {
log.message = 'Invalid config';
$log.error(log);
return;
}
if(!userState.userProperties) {
userState.userProperties = {};
}
if(!userState.contentProperties) {
userState.contentProperties = {};
}
log.message = 'User Properties: ' + JSON.stringify(userState.userProperties);
$log.debug(log);
log.message = 'Content Properties: ' + JSON.stringify(userState.contentProperties);
$log.debug(log);
log.message = 'Loaded user data from firebase';
$log.debug(log);
readyCallback();
},
function() {
log.message = 'Unable to load user data from firebase';
$log.error(log);
}
);
};
// Returns the initial tip configuration
this.getConfig = function() {
return config;
};
// Set the value of a user property
// A user property is something about the user himself
this.setUserProperty = function(property, value) {
if(!userState.userProperties) {
userState.userProperties = {};
}
userState.userProperties[property] = value;
userState.$save();
$rootScope.$broadcast('user-property-change', property);
};
// Get the value of a user property
this.getUserProperty = function(property) {
if(userState.userProperties) {
return userState.userProperties[property];
}
};
// Set the value of a user content property
// A content property is something about a particular peice of content for a particular user
this.setContentProperty = function(contentName, property, value) {
if(!userState.contentProperties[contentName]) {
userState.contentProperties[contentName] = {};
}
userState.contentProperties[contentName][property] = value;
userState.$save();
$rootScope.$broadcast('content-property-change', contentName, property);
};
// Increment a count property on the user state for a given tip
this.incrementContentProperty = function(contentName, property) {
if(!userState.contentProperties[contentName]) {
userState.contentProperties[contentName] = {};
}
if(!userState.contentProperties[contentName][property]) {
userState.contentProperties[contentName][property] = 0;
}
userState.contentProperties[contentName][property]++;
userState.$save();
$rootScope.$broadcast('content-property-change', contentName, property);
};
// Returns the user state for a given tip and property
this.getContentProperty = function(contentName, property) {
if(userState.contentProperties) {
var t = userState.contentProperties[contentName];
if(t) {
return t[property];
}
}
};
});
我读过Jasmine中的异步支持,但由于我对JavaScript的单元测试还比较陌生,所以无法使它工作
我收到一个错误:
在指定的超时内未调用异步回调
jasmine.DEFAULT\u超时\u间隔
有人能帮我提供一些解释我的代码的工作示例吗?我建议您将
设置超时
替换为$timeout
,以加速您的规范套件。你需要成为你的规范套件的一部分,以使它按预期的方式工作,但这似乎已经在你的规范中得到了注意。好东西
然后,为了使规范的异步特性“消失”,您需要调用:
其中,延迟
是可选的
- 如果没有传递延迟,所有挂起的异步任务(在角度世界中)都将完成它们正在做的事情
- 如果通过延迟,则指定延迟内的所有挂起任务都将完成。超出指定延迟的部分将保持“待定”状态
done
回调,并按如下方式编写测试:
describe('after being initialized', function () {
var $timeout;
beforeEach(function () {
// Unable to get this working because the callback is never called
userData.init();
inject(function ($injector) {
$timeout = $injector.get('$timeout');
});
}));
it('should have a valid config', function () {
$timeout.flush();
// callback should've been called now that we flushed().
expect(Object.keys(userData.getConfig()).length).toEqual(0);
});
});
您正在使用什么承诺
实现?我看到了一个对Promise.all的调用,但是为了继续我的回答,我假设它相当于$q.all。运行$timeout.flush
应负责解析这些值
如果你想在《茉莉花》中写下被拒绝/解决的承诺价值的期望,我会研究一些东西,比如让它干净漂亮,但除非你可以这样做:
// $q
function get () {
var p1 = $timeout(function () { return 'x'; }, 250);
var p2 = $timeout(function () { return 'y'; }, 2500);
return $q.all([p1, p2]);
}
// expectation
it('is correct', function () {
var res;
get().then(function (r) {
res = r;
});
$timeout.flush(2500);
expect(res).toEqual(['x', 'y']);
});
根据您的设置,您可能需要也可能不需要删除/监视(取决于您的框架对间谍的定义)与本地config
变量相关的承诺,但我认为这完全是另一回事
我对$firebase(something).$asObject.$loaded一点也不熟悉——因此,我可能在这里遗漏了一些东西,但假设它“像任何其他承诺一样”起作用,你应该很乐意去做
解决承诺时调用readyCallback。您需要解决承诺并触发摘要,请参阅感谢您的回复如何在不更改服务的情况下进行更正,请帮助我解决此问题您不需要更改源代码。您应该对config和userState使用mock,并在其$loaded函数上返回已解析的承诺,然后在测试中调用$scope.$apply。如果你对你的代码进行了修改,我可以在那里展示。littel明白你的意思了,你能帮我做一个测试用例吗?删除“done”参数将非常有帮助。[这个问题][1]的答案。[1]:
// $q
function get () {
var p1 = $timeout(function () { return 'x'; }, 250);
var p2 = $timeout(function () { return 'y'; }, 2500);
return $q.all([p1, p2]);
}
// expectation
it('is correct', function () {
var res;
get().then(function (r) {
res = r;
});
$timeout.flush(2500);
expect(res).toEqual(['x', 'y']);
});