Why is $ q.when not allowed when transferring a third-party promise when using ngMock?

Given the following service:

angular.module('app', [])
  .service('promisey', function ($q) {
    this.cakey = function () {
      return $q.when('brownie')
    }
    this.fruity = function () {
      return $q.when(Promise.resolve('apple'))
    }
  })

... and the following test:

var self = this

describe('when', function () {
  var promisey
  var $rootScope

  beforeEach(module('app'))
  beforeEach(inject(function(_$rootScope_, _promisey_) {
    promisey = _promisey_
    $rootScope = _$rootScope_
  }))

  it('should give cakes', function (done) {
    promisey.cakey()
      .then(function (cake) {
        expect(cake).toBe('brownie')
      })
      .catch(self.fail.bind())
      .finally(done)
    $rootScope.$apply()
  })

  it('should give fruit', function (done) {
    promisey.fruity()
      .then(function (cake) {
        // XXX: does not resolve
        expect(cake).toBe('apple')
      })
      .catch(self.fail.bind())
      .finally(done)
    $rootScope.$apply()
  })
})

... when ngMockreceived, promisey.fruity()will never be allowed. If I do not send ngMock(and the handle itself angular.injector), the test will be resolved, as expected. Why is this?

An abbreviated test case for this question can be found at tlvince / q-when-reduced-test-case .

+4
source share
1 answer

, native promises , Angular. Angular promises evalAsync . - promises - native promises -, " ".

$rootScope.$apply, " evalAsync" , "" promises.

"" native promises, , .

ngMock Mocha:

  it('should give fruit', function () { // no done
    return promisey.fruity() // see return here
      .then(function (cake) {
        expect(cake).toBe('apple')
      });
      // no digest, no `done`
  });

, :)

+3

Source: https://habr.com/ru/post/1607653/


All Articles