Jquery 如何用Jasmine测试我的回调函数?
我有一个角度服务类:- //从firebase加载用户数据Jquery 如何用Jasmine测试我的回调函数?,jquery,angularjs,jasmine,karma-jasmine,Jquery,Angularjs,Jasmine,Karma Jasmine,我有一个角度服务类:- //从firebase加载用户数据 this.init = function(readyCallback) { var log = angular.extend({}, this._log); log.funct = 'init'; var fireRef = new Firebase('https://luminous-inferno-1740.firebaseio.com/' + $rootScope.clientName); config = $f
this.init = function(readyCallback) {
var log = angular.extend({}, this._log);
log.funct = 'init';
var fireRef = new Firebase('https://luminous-inferno-1740.firebaseio.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);
}
);
};
this.init=函数(readyCallback){
var log=angular.extend({},this.\u log);
log.funct='init';
var fireRef=新的Firebase('https://luminous-inferno-1740.firebaseio.com/'+$rootScope.clientName);
config=$firebase(fireRef.child('config'))。$asObject();
userState=$firebase(fireRef.child('userState').child($rootScope.userName))。$asObject();
Promise.all([config.$loaded(),userState.$loaded()])。
然后(
函数(){
if(config==null | | Object.keys(config.length<4){
log.message='无效配置';
$log.error(log);
回来
}
如果(!userState.userProperties){
userState.userProperties={};
}
如果(!userState.contentProperties){
userState.contentProperties={};
}
log.message='User Properties:'+JSON.stringify(userState.userProperties);
$log.debug(log);
log.message='contentProperties:'+JSON.stringify(userState.contentProperties);
$log.debug(log);
log.message='从firebase加载的用户数据';
$log.debug(log);
readyCallback();
},
函数(){
log.message='无法从firebase加载用户数据';
$log.error(log);
}
);
};
我正在尝试使用jasmine对该服务进行单元测试:-
我的单元测试是:-
// load the service's module
beforeEach(function() {
module('triggerTips');
});
// instantiate service
var userData;
var $firebase;
var log;
var $rootScope;
beforeEach(inject(function (_userData_, _$rootScope_, $log, _$firebase_, $http) {
userData = _userData_;
$firebase=_$firebase_;
log = $log;
$rootScope = _$rootScope_;
$rootScope.clientName = 'testClient';
$rootScope.userName = 'userDataTest';
$rootScope.userGroupName = 'testGroup';
$rootScope.env = 'dev';
}));
it('should load correctly', function () {
expect(!!userData).toBe(true);
});
describe('initial configuration for the test user', function () {
beforeEach(function(done){
var config = {
'Apache 404' : {
content : {
type : 'tip',
template : 'yesNo',
text : 'Are you searching for status code 404?',
yesText : 'Try our more accurate <a href="https://www.loggly.com/docs/search-query-language/#field_names" target="_blank">field search</a>',
attachTo : '#inputBox right',
yesActions : {
0 : {
type : 'replaceSubstring',
target : '#inputBox',
value : 'apache.status:404',
match : '404'
}
}
},
conditions : {
0 : {
type : 'valueChange',
target : '#inputBox',
textMatch : '(^|([\\s]+))404(([\\s]+)|$)',
preventSubmit : true
},
1 : {
type : 'contentPropertyLessThan',
propertyName : 'timesShown',
compareVal : 3
}
}
}
};
var fireRef = new Firebase('https://luminous-inferno-1740.firebaseio.com/' + $rootScope.clientName);
var fireSync = $firebase(fireRef);
fireSync.$set({'config' : config});
userData.init();
jasmine.DEFAULT_TIMEOUT_INTERVAL = 2000;
});
it('should have a success log', function(done) {
setTimeout(function() {
expect(log.debug.logs.length > 9).toBe(true);
expect(log.debug.logs.length).toBeGreaterThan(2);
done();
}, 2500);
});
it('should have a valid config', function () {
expect(Object.keys(userData.getConfig()).length == 1).toBe(true);
});
});
//加载服务的模块
beforeach(函数(){
模块(“触发器提示”);
});
//实例化服务
var用户数据;
var$firebase;
var测井;
var$rootScope;
beforeach(注入(函数(userData、$rootScope、$log、$firebase、$http){
用户数据=\u用户数据\u0;
$firebase=\$firebase;
log=$log;
$rootScope=\u$rootScope;
$rootScope.clientName='testClient';
$rootScope.userName='userDataTest';
$rootScope.userGroupName='testGroup';
$rootScope.env='dev';
}));
它('应正确加载',函数(){
expect(!!userData).toBe(true);
});
描述('测试用户的初始配置',功能(){
每次之前(功能完成){
变量配置={
“Apache404”:{
内容:{
键入:“提示”,
模板:“yesNo”,
文本:“您正在搜索状态代码404吗?”,
yesText:'尝试我们更准确的方法',
附件:“#输入框右侧”,
是的:{
0 : {
类型:“replaceSubstring”,
目标:“#输入框”,
值:“apache.status:404”,
比赛:“404”
}
}
},
条件:{
0 : {
类型:“valueChange”,
目标:“#输入框”,
textMatch:“(^|([\\s]+))404(([\\s]+)|$)”,
真的吗
},
1 : {
类型:“contentPropertyLessThan”,
propertyName:“时间显示”,
比较:3
}
}
}
};
var fireRef=新的Firebase('https://luminous-inferno-1740.firebaseio.com/'+$rootScope.clientName);
var fireSync=$firebase(fireRef);
fireSync.$set({'config':config});
init();
jasmine.DEFAULT\u TIMEOUT\u INTERVAL=2000;
});
它('应该有一个成功日志',函数(完成){
setTimeout(函数(){
expect(log.debug.logs.length>9).toBe(true);
expect(log.debug.logs.length).toBeGreaterThan(2);
完成();
}, 2500);
});
它('应该有一个有效的配置',函数(){
expect(Object.keys(userData.getConfig()).length==1.toBe(true);
});
});
我是新手,我收到一个错误:
有人能帮我提供代码的工作示例吗?您缺少收到的错误消息。另外,如果你简化你正在使用的代码,回答起来会容易得多。你能帮我吗。我对Karma很陌生,除非你简化代码片段,否则我帮不了你。