2017-04-11 26 views
0

這裏的代碼存在https://review.openstack.org/#/c/418828/但我會進入更詳細如下:角/茉莉:錯誤:預期間諜已被稱爲

我寫測試這個特定的代碼塊:https://review.openstack.org/#/c/418828/26/openstack_dashboard/static/app/core/network_qos/qos.service.js

(function() { 
    "use strict"; 

    angular.module('horizon.app.core.network_qos') 
    .factory('horizon.app.core.network_qos.service', qosService); 

    qosService.$inject = [ 
    '$filter', 
    'horizon.app.core.openstack-service-api.neutron', 
    'horizon.app.core.openstack-service-api.userSession' 
    ]; 

    /* 
    * @ngdoc factory 
    * @name horizon.app.core.network_qos.service 
    * 
    * @description 
    * This service provides functions that are used through the QoS 
    * features. These are primarily used in the module registrations 
    * but do not need to be restricted to such use. Each exposed function 
    * is documented below. 
    */ 
    function qosService($filter, neutron, userSession) { 
    var version; 

    return { 
     getPolicyPromise: getPolicyPromise, 
     getPoliciesPromise: getPoliciesPromise 
    }; 

    /* 
     * @ngdoc function 
     * @name getPoliciesPromise 
     * @description 
     * Given filter/query parameters, returns a promise for the matching 
     * policies. This is used in displaying lists of policies. In this case, 
     * we need to modify the API's response by adding a composite value called 
     * 'trackBy' to assist the display mechanism when updating rows. 
     */ 
    function getPoliciesPromise(params) { 
     return userSession.get().then(getQoSPolicies); 

     function getQoSPolicies() { 
     return neutron.getQoSPolicies(params).then(modifyResponse); 
     } 

     function modifyResponse(response) { 
     return {data: {items: response.data.items.map(modifyQos)}}; 

     function modifyQos(policy) { 
      policy.trackBy = policy.id; 
      policy.apiVersion = version; 
      policy.name = policy.name || policy.id; 
      return policy; 
     } 
     } 
    } 

    /* 
    * @ngdoc function 
    * @name getPolicyPromise 
    * @description 
    * Given an id, returns a promise for the policy data. 
    */ 
    function getPolicyPromise(identifier) { 
     neutron.getVersion().then(setVersion); 
     return neutron.getQosPolicy(identifier).then(modifyResponse); 

     function modifyResponse(response) { 
     response.data.apiVersion = version; 
     return {data: response.data}; 
     } 
    } 
    } 

})(); 

這是我目前的測試文件:

(function() { 
    "use strict"; 

    describe('qosService', function() { 
    var service; 
    beforeEach(module('horizon.app.core')); 
    beforeEach(inject(function($injector) { 
     service = $injector.get('horizon.app.core.network_qos.service'); 
    })); 

    describe('getPoliciesPromise', function() { 
     it("provides a promise that gets translated", inject(function($q, $injector, $timeout) { 
     var neutron = $injector.get('horizon.app.core.openstack-service-api.neutron'); 
     var session = $injector.get('horizon.app.core.openstack-service-api.userSession'); 
     var deferred = $q.defer(); 
     var deferredSession = $q.defer(); 
     spyOn(neutron, 'getQoSPolicies').and.returnValue(deferred.promise); 
     spyOn(session, 'get').and.returnValue(deferredSession.promise); 
     var result = service.getPoliciesPromise({}); 
     deferred.resolve({ 
      data: { 
      items: [{id: 123, name: 'policy1'}] 
      } 
     }); 
     $timeout.flush(); 
     expect(neutron.getQoSPolicies).toHaveBeenCalled(); 
     expect(result.$$state.value.data.items[0].name).toBe('policy1'); 
     })); 
    }); 

    }); 

})(); 

當我進行的測試,我目前得到的錯誤,他說:

Expected spy getQoSPolicies to have been called. 

正如你所看到的,getQoSPolicies是絕對被調用的。如果任何人都可以看到測試出了什麼問題,那麼我將非常感謝!提前謝謝了!

回答

2

你應該解決以下與neutron一個沿承諾(deferredSession),否則不會去userSession.get().then(getQoSPolicies)裏面.then的:

var deferredSession = $q.defer(); 
spyOn(session, 'get').and.returnValue(deferredSession.promise); 
... 
... 

deferredSession.resolve({}); 
deferred.resolve(...); 
$timeout.flush(); 

解決,隨着現有的和你期望它應該工作!

+0

非常感謝,完美的作品! :) –

+0

@BethElwell很高興我可以幫助! :) – tanmay