Lukasz Madon
Lukasz Madon

Reputation: 15004

Promise callback doesn't execute while running unit test

I want to test following controller:

angular.module('myApp').controller('OrderController', ['$scope', 'retryRequest', 'ORDER_STATUS_URL', 'MAX_RETRY',
 function ($scope, retryRequest, ORDER_STATUS_URL, MAX_RETRY) {
  retryRequest(ORDER_STATUS_URL, 'GET', MAX_RETRY).then(function(data) {
    $scope.order = data;
  }).catch(function(data) {
    $scope.error = data.ErrorMessage;
  });
}]);

test:

describe('Order Controller', function () {

  beforeEach(module('myApp'));

  var $controller, $q;
  beforeEach(inject(function (_$controller_, _$q_) {
    $controller = _$controller_;
    $q = _$q_;
  }));

  describe('When I access order data', function() {
    it('I expect Id to be set', function() {
      var mockRetryRequest = function(){
        var deferred = $q.defer();
        deferred.resolve({ 'OrderId': 'foo' });
        return deferred.promise;
      };
      var $scope = {};
      var controller = $controller('OrderController', { $scope: $scope, retryRequest: mockRetryRequest });
      assert.equal($scope.order.OrderId, 'foo');
    });
  });
});

which result in $scope.order being undefined. The mock is injected, but the promise doesn't call the callback (then).

Possible candidate is mocha setup.

bower.json

  "name": "client",
  "private": true,
  "dependencies": {
    "chai": "~1.8.0",
    "mocha": "~1.14.0",
    "angular": "1.3.11",
    "angular-mocks": "1.3.11",
    "sinon": "~1.12.2"

Upvotes: 0

Views: 88

Answers (1)

Lukasz Madon
Lukasz Madon

Reputation: 15004

The digest loop doesn't run. Call $rootScope.$digest();

Upvotes: 1

Related Questions