0

I'm starting with TDD , and am having trouble with test functions using promises , below I will list my scenario :

My Function:

(function () {
  'use strict';
  angular.module('com.pentaxial.utils.modais', [])
  .service('Modal', ['$ionicModal', '$q', function ($ionicModal, $q) {

    var self = this;

    self.criar = function ($scope, template) {
      var deferred = $q.defer();

      if(!template)  deferred.reject(new Error("Template cannot be null"));

      $ionicModal.fromTemplateUrl(template, {
        scope: $scope,
        animation: 'slide-in-up'
      }).then(function (modal) {
        deferred.resolve(modal)
      }, function(err){
        deferred.reject(err);
      });

      return deferred.promise;
    }

    self.destruir = function (modal) {
      modal.hide();
      modal.remove();
      return undefined;
    }
  }])
})();

it should be a call to excutar the self.criar test ?

I try:

it('Retorno com template definido', function(){     Modais.criar(scope, 'view/modal/cropper.html').then(function(modal){    expect(modal).toBeDefined();   },function(err){    expect(err).toBeDefined();   }) })

But not work.

Code Coverage

1fabiopereira
  • 542
  • 2
  • 4
  • 18
  • Possible duplicate of [Unit-test promise-based code in Angular](http://stackoverflow.com/questions/16081586/unit-test-promise-based-code-in-angular) – Heretic Monkey Aug 22 '16 at 23:15
  • See also [the official documentation](https://docs.angularjs.org/guide/unit-testing#testing-promises) – Heretic Monkey Aug 22 '16 at 23:16
  • I already understood this but even I using mock my code coverage is not complete , look at the link: http://i.stack.imgur.com/YkF93.png – 1fabiopereira Aug 23 '16 at 17:37
  • You can't expect a single test to cover all of the branches in your code. In fact that is the opposite of how tests should work. You should have a single test that tests whether the promise is rejected if no template is provided. You should have a single test that tests whether `$ionicModal.fromTemplateUrl` is called with the correct arguments. And so on, until you have no more code to test. Also, please take a look at [ask] and how to create a [mcve]. One key thing: define what "not work" means. – Heretic Monkey Aug 23 '16 at 17:41
  • Ok thanks for help. – 1fabiopereira Aug 24 '16 at 20:51

0 Answers0