2015-09-03 3 views
0

Je rencontre des difficultés dans mon test pour un contrôleur modal ionique. Le problème (ou du moins le problème sur lequel je me concentre) se moque de la fonction $ ionicModal.fromTemplateUrl. Selon la documentation ionique, il est supposé renvoyer une promesse qui se résout en une instance du modal.

Voici mon usine:

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

Et voici mon test:

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

Je suis aussi incertain si ma fonction getController retourne correctement le contrôleur. C'est la première fois que je travaille avec $ ionicModal, donc les pointeurs sont appréciés. Merci.

Répondre

0

FIXES:

Je ne ai pas eu le DFD mis en place correctement. J'ai aussi eu l'ensemble de spectacle en fonction de mockIonicPopup quand c'est une fonction de mockModal.

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