Menu

#10 Document Unit Testing with Angular Modal Service

open
nobody
None
2015-02-02
2014-10-14
Anonymous
No

Originally created by: dwmkerr

It would be extremely useful to document how unit testing works with Angular Modal Service.

Discussion

  • Anonymous

    Anonymous - 2015-01-28

    Originally posted by: alllx

    I made a mock of ModalService for test:

    describe('some test', function () {
      var $httpBackend, scope, modalServiceMock, modalInstance, someController;
      beforeEach(function () {
        inject(function (_$httpBackend_, $rootScope, $controller) {
          $httpBackend = _$httpBackend_;
          scope = $rootScope.$new();
          // mock modal service
          modalServiceMock = {
            showModal: function (options) {
              return {
                then: function (callback) {
                  modalInstance = {
                    scope: {},
                    close: {
                      then: function (callback) {
                        this.callback = callback;
                      }
                    }
                  };
                  callback(modalInstance);
                }
              };
            }
          };
          someController = $controller('controllerName', {
            $scope: scope,
            ModalService: modalServiceMock
          });
      });
      it('open and close modal', function () {
        scope.methodInitingModal();
        // some checks here modal is showed
        modalInstance.close.callback('confirm');
        // make any check modal close callback has been called with passed result string
      });
    
    });
    
     
  • Anonymous

    Anonymous - 2015-02-02

    Originally posted by: dwmkerr

    Looks great @alllx thanks for sharing this, I'll use this as a base when I write up the unit testing samples!

     

Log in to post a comment.