具有多个消化循环的角度Karma单元测试

时间:2014-09-18 15:38:11

标签: angularjs unit-testing karma-jasmine

我在CRUD对象上有一个手表,我在我的选择对象上有手表。在我的单元测试中,我希望person对象的初始化初始化选择对象,并且我希望更改select对象以更改person对象。我的单元测试失败了,因为我无法在一次测试中运行两个摘要周期。这意味着所有人员和选择对象的更改将同时处理,覆盖其中一个或另一个。请帮忙。

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();

1 个答案:

答案 0 :(得分:0)

此测试的代码是包含select opts和值的对象,如下所示:

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;
};