2016-04-28 131 views
2

如何測試Restangular.extendModel茉莉例如:Restangular extendModel茉莉花

控制器:

_this.populateSecurityGroups = function() { 
    var netid = _this.instance.vm_fields.cloud_network * 1; 
    CloudNetworksFactory.one(_this.provider, netid).then(
      function (net) { 
      net.security_groups(_this.provider).then(
       function (sgs) { _this.security_groups = sgs; }, 
       function (err) {} 
      ); 
      }, 
      function (err) {} 
    ); 
    }; 

廠:

var all = function (provider) { 
     return provider.getList('cloud_networks'); 
    }; 

    var one = function (provider, id) { 
     var defered = $q.defer(); 
     this.all(provider).then(
     function (nets) { 
      var res = _.find(nets.data, { id: id }); 
      defered.resolve(res); 
     }, 
     defered.reject 
    ); 
     return defered.promise; 
    }; 

    Restangular.extendModel('cloud_networks', function (model) { 
     model.security_groups = function (provider) { 
     var _this = this; 
     var defered = $q.defer(); 
     provider.security_groups().then(
      function (sgs) { 
      var res = ...     
      defered.resolve(res); 
      }, 
      defered.reject 
     ); 

     return defered.promise; 
     }; 

     return model; 
    }); 

我無法測試這個功能,第一個錯誤是:

未定義不是(評價 'r.getList( 「cloud_networks」)')

未定義不是(評價 'e.security_groups')

這是開始時的對象的構造我的測試,我不能嘲笑restangular.extendModel:

it('should test populateCloudSubnets properly', function (done) { 
     ctrl.instance.vm_fields.cloud_network = 1; 
     ctrl.populateSecurityGroups().then(function(){ 
     .... 
     }, function(err){ 
     expect(err).toBe(null); 
     done(); 
     }); 

     $httpBackend.flush(); 
    }); 

回答

0

爲了茉莉測試restangular.extendModel你應該使用Restangular.restangularizeElement

回吐例如,你有:

Restangular.extendModel('cloud_networks', function (model) { 
    model.security_groups = function() {} 
} 

你可以這樣做:

it('does something' function() { 
    var model = {id: 1}; 
    Restangular.restangularizeElement(null, model, 'cloud_networks'); 

    // You can test model.security_groups() in here 
}