Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS/Ionic/SinonChai-如何模拟$ionicModal.fromTemplateUrl函数_Javascript_Angularjs_Unit Testing_Ionic_Sinon Chai - Fatal编程技术网

Javascript AngularJS/Ionic/SinonChai-如何模拟$ionicModal.fromTemplateUrl函数

Javascript AngularJS/Ionic/SinonChai-如何模拟$ionicModal.fromTemplateUrl函数,javascript,angularjs,unit-testing,ionic,sinon-chai,Javascript,Angularjs,Unit Testing,Ionic,Sinon Chai,我在测试离子模态控制器时遇到问题。我所关注的问题或至少是模拟$ionicModal.fromTemplateUrl函数。根据ionic文档,它应该返回一个承诺,并解析为模态的实例 这是我的工厂: (function() { 'use strict'; angular.module('penta.app.main').factory('addEquipment', AddEquipment); function AddEquipment($rootScope, $ionicModa

我在测试离子模态控制器时遇到问题。我所关注的问题或至少是模拟$ionicModal.fromTemplateUrl函数。根据ionic文档,它应该返回一个承诺,并解析为模态的实例

这是我的工厂:

(function() {
  'use strict';

  angular.module('penta.app.main').factory('addEquipment', AddEquipment);

  function AddEquipment($rootScope, $ionicModal) {
    return {
      openModal: function() {
        var scope = $rootScope.$new(true);
        scope.controller = new AddEquipmentController(scope, $ionicModal);
      }
    };

    function AddEquipmentController(scope, $ionicModal) {
      var controller = this;

      $ionicModal.fromTemplateUrl('app/tracking/activityLog/addItems/equipment/addEquipment.html', {
        scope: scope,
        animation: 'slide-in-up'
      }).then(function(modal) {
        controller.modal = modal;
        controller.openModal();
      });


      controller.openModal = function() {
        controller.modal.show();
      };

      controller.closeModal = function() {
        controller.modal.hide();
      };

      return controller;
    }
  }
})();
这是我的测试:

(function() {
  'use strict';

  describe('AddEquipment', function() {
    var controllerConstructor;
    var addEquipment;
    var mock;
    var mockIonicModal;
    var mockModal;
    var scope;
    var dfd;

    beforeEach(module('penta.app.main'));
    beforeEach(module('unitTest'));
    beforeEach(module('app/tracking/activityLog/addItems/equipment/addEquipment.html'));

    beforeEach(function() {
      mockModal = sinon.stub({
        show: function() {
        },

        hide: function() {
        }
      });

      mockIonicModal = sinon.stub({
        fromTemplateUrl: function() {
        },

        then: function() {
        }
      });
      mockIonicModal.fromTemplateUrl.returns(mockModal);
    });

    beforeEach(function() {
      module(function($provide) {
        $provide.value('$ionicModal', mockIonicModal);
      });
    });

    beforeEach(inject(function($rootScope, $controller, $q, ptiMock) {
      controllerConstructor = $controller;
      dfd = $q.defer();
      scope = $rootScope.$new();
      mock = ptiMock;
      mockModal.$promise = dfd.promise;
    }));

    beforeEach(inject(function(_addEquipment_) {
      addEquipment = _addEquipment_;
    }));

    it('exists', function() {
      expect(addEquipment).to.exist;
    });

    describe('open', function() {
      it.only('opens the modal', function() {
        addEquipment.openModal();
        dfd.resolve(mockModal);
        scope.$digest();

        expect(mockIonicModal.show.calledOnce).to.be.true;
      });
    });

    function getController() {
      return mockIonicModal.fromTemplateUrl.lastCall.args[0].scope.controller;
    }
  });
})();
我也不确定getController函数是否会正确返回控制器。这是我第一次使用$ionicModal,因此非常感谢您的指点。谢谢。

修复:

我没有正确设置dfd。另外,当它是mockModal的函数时,我将它设置为mockIonicPopup的函数

(function() {
  'use strict';

  describe('AddEquipment', function() {
    var controllerConstructor;
    var addEquipment;
    var mock;
    var mockIonicModal;
    var mockModal;
    var scope;
    var dfd;

    beforeEach(module('penta.app.main'));
    beforeEach(module('unitTest'));
    beforeEach(module('app/tracking/activityLog/addItems/equipment/addEquipment.html'));

    beforeEach(function() {
      mockModal = sinon.stub({
        show: function() {
        },

        hide: function() {
        }
      });

      mockIonicModal = sinon.stub({
        fromTemplateUrl: function() {
        },

        then: function() {
        }
      });
    });

    beforeEach(function() {
      module(function($provide) {
        $provide.value('$ionicModal', mockIonicModal);
      });
    });

    beforeEach(inject(function($rootScope, $controller, $q, ptiMock) {
      controllerConstructor = $controller;
      dfd = $q.defer();
      scope = $rootScope.$new();
      mock = ptiMock;
      mockIonicModal.fromTemplateUrl.returns(dfd.promise);
    }));

    beforeEach(inject(function(_addEquipment_) {
      addEquipment = _addEquipment_;
    }));

    it('exists', function() {
      expect(addEquipment).to.exist;
    });

    describe('openModal', function() {
      it.only('opens the modal', function() {
        addEquipment.openModal();
        dfd.resolve(mockModal);
        scope.$digest();

        expect(mockModal.show.calledOnce).to.be.true;
      });
    });

    function getController() {
      return mockIonicModal.fromTemplateUrl.lastCall.args[0].scope.controller;
    }
  });
})();