【问题标题】:angular/jasmine: Error: Expected spy to have been called角/茉莉花:错误:预期的间谍已被调用
【发布时间】:2017-04-11 18:59:56
【问题描述】:

代码在这里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 肯定是被调用的。如果有人能看到测试有什么问题给我这个错误,那将不胜感激!非常感谢!

【问题讨论】:

    标签: javascript angularjs jasmine


    【解决方案1】:

    您应该解决以下承诺 (deferredSession) 和 neutron 之一,否则它不会进入 userSession.get().then(getQoSPolicies) 的 .then:

    var deferredSession = $q.defer();
    spyOn(session, 'get').and.returnValue(deferredSession.promise);
    ...
    ...
    
    deferredSession.resolve({});
    deferred.resolve(...);
    $timeout.flush();
    

    与现有的一起解决它,它应该可以按您的预期工作!

    【讨论】:

    • @BethElwell 很高兴我能帮上忙! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 2014-01-25
    相关资源
    最近更新 更多