Angularjs 具有多个摘要周期的角业力单元测试
我的CRUD对象person上有一块手表,我的select对象上有一块手表。在我的单元测试中,我想初始化person对象来初始化select对象,我想更改select对象来更改person对象。我的单元测试失败,因为我无法在一个测试中运行两个摘要周期。这意味着在处理person和select对象中的所有更改时,会同时覆盖其中一个对象。请帮忙Angularjs 具有多个摘要周期的角业力单元测试,angularjs,unit-testing,karma-jasmine,Angularjs,Unit Testing,Karma Jasmine,我的CRUD对象person上有一块手表,我的select对象上有一块手表。在我的单元测试中,我想初始化person对象来初始化select对象,我想更改select对象来更改person对象。我的单元测试失败,因为我无法在一个测试中运行两个摘要周期。这意味着在处理person和select对象中的所有更改时,会同时覆盖其中一个对象。请帮忙 scope.person = samplePersonPutData; scope.$apply(); scope.selects.medicalCondi
scope.person = samplePersonPutData;
scope.$apply();
scope.selects.medicalConditions.values.push({id:'02', text:'Cold'});
scope.selects.courses.values = [];
scope.selects.nextOfKin.value = {id:'1111111112', text:'Bob'};
scope.$apply(); //this doesn't run
$httpBackend.expectPUT(/people\/([0-9a-fA-F]{24})$/,samplePersonPutResponse).respond();
scope.update();
$httpBackend.flush();
此测试的代码是包含select选项和值的对象,如下所示:
function TagSelect(baseObj, sub, field, tags, $scope){
Select2.call(this, sub, field);
console.log("TagSelect Constructing "+baseObj+","+sub+","+field);
this.tags = tags;
this.scope = $scope;
this.opts = {
initSelection: function (element, callback) {
callback(element);
},
multiple: true,
tags: function() {
return tags;
},
width: '100%'
};
this.values = [];
this.new = '';
this.baseObj = baseObj;
this.scope.$watch('selects.'+this.field+'.values',this.selectWatch(this, this.baseObj));
this.scope.unwatchBaseObj = this.scope.$watch(baseObj, this.baseObjWatch(this));
}
TagSelect.prototype = Object.create(Select2.prototype);
TagSelect.prototype.constructor = TagSelect;
TagSelect.prototype.selectWatch = function(self, objectName){
return function(){
self.setDO(objectName);
};
};
TagSelect.prototype.baseObjWatch = function(self){
return function(){
self.setValue(self.scope[self.baseObj]);
console.log('watch baseObj self.field:'+self.field);
};
};
TagSelect.prototype.setDO = function(baseObj){
var i;
if(this.values.length === 0)return;
//var watchers = this.scope.$$watchers;
//this.scope.$$watchers = [];
console.log("setting DO for "+this.field+" values.length"+this.values.length);
if(!this.scope[baseObj]){
this.scope[baseObj] = {};
}
if(this.sub){
if(!this.scope[baseObj][this.sub]){
this.scope[baseObj][this.sub] = {};
}
this.scope[baseObj][this.sub][this.field] = [];
for(i = 0; i < this.values.length; i++){
this.scope[baseObj][this.sub][this.field].push(this.values[i].text);
}
}else{
this.scope[baseObj][this.field] = [];
for(i = 0; i < this.values.length; i++){
this.scope[baseObj][this.field].push(this.values[i].text);
}
}
//this.scope.$$watchers = watchers;
};
TagSelect.prototype.setValue = function(object){
var i;
console.log("in setValue");
//var watchers = this.scope.$$watchers;
//this.scope.$$watchers = [];
//this.scope.unwatchBaseObj();
if(this.sub){
console.log("setting select for "+this.field);
if(object[this.sub] && object[this.sub][this.field]){
this.values = [];
console.log("setting select for "+this.field+" with length:"+object[this.sub][this.field].length+" values.length:"+this.values.length);
for(i = 0; i < object[this.sub][this.field].length; i++){
this.values.push({id: object[this.sub][this.field][i], text: object[this.sub][this.field][i]});
}
}
}else{
if(object[this.field]){
this.values = [];
console.log("setting select for "+this.field+" with length:"+object[this.field].length);
for(i = 0; i < object[this.field].length; i++){
this.values.push({id: object[this.field][i], text: object[this.field][i]});
}
}
}
//this.scope.$$watchers = watchers;
};
你说的“这不运行”是什么意思?有什么错误吗?我的意思是命令什么都不做。没有手表被解雇。你能分享这个测试应该测试的源代码吗。你确定这只是测试代码的问题吗?我类似的创建单元测试是有效的,因为person对象是空的,在这种情况下只需要一个摘要周期。