dwmkerr / angular-modal-service

Modal service for AngularJS - supports creating popups and modals via a service.
MIT License
626 stars 321 forks source link

Document Unit Testing with Angular Modal Service #10

Open dwmkerr opened 10 years ago

dwmkerr commented 10 years ago

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

alllx commented 9 years ago

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

});
dwmkerr commented 9 years ago

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

dwmkerr commented 7 years ago

Note: When documenting the testing approach, also use:

https://github.com/dwmkerr/angular-modal-service/issues/40