1

I am trying to run a jasmine test in visual studios using chutzpah/test explorer.

I have a controller that looks like this:

function ctrl($scope, $timeout, resolvedData) {
  var vm = this;
  vm.data;
  init();

  function init() {
    if(resolvedData.length > 0) {
      $timeout(function() {
        vm.data = resolvedData;
      }, 0);
    };
  };
}

My test file looks like this:

describe('ctrl', function() {
  var ctrl,
      resolvedData,
      $controller,
      $timeout,
      $scope;
  var mockResolvedData = [{}, {}, {}];

  beforeEach(function() {
    module('app');
    inject(function(_$controller_, _$rootScope_, _$timeout_) {
      $scope = _$rootScope.$new();
      $controller = _$controller_;
      $timeout = _$timeout_;
      resolvedData = mockResolvedData;
      ctrl = $controller('ctrl', {$scope: $scope, resolvedData: resolvedData, $timeout: $timeout})
    })
  })

  it('should set data when the controller loads', function() {
    $timeout.flush();
    expect(ctrl.data).toEqual(mockResolvedData);
  })
})

When I add the $timeout.flush(), I get this error:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!

Taking out the $timeout.flush() gives me this error:

Expected undefined to equal [ { }, { }, { } ].

Adding $scope.$apply() before the expect (instead of $timeout.flush()) gives me this:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!

Is there something else that I am missing? I want to test that vm.data gets set when the controller loads after the function from $timeout is called but no luck.

EDIT 1: I created a plunker and it's working fine in their. I'm stumped. http://plnkr.co/edit/Ae28AyjKwCTen1nHKZAo?p=preview

EDIT 2: I replaced $timeout.flush() with $timeout.verifyNoPendingTasks() like this:

it('should do set data when the controller loads', function() {
  $timeout.verifyNoPendingTasks();
  expect(ctrl.data)toEqual(mockResolvedData);
})

And now I have this error:

Error: Deferred tasks to flush (2): {id: 0, time: 0}, {id: 1, time: 0}

So we can clearly see that there are tasks to be flushed but when I flush them, I get that digest error.

6
  • i've read this: stackoverflow.com/questions/17418716/… and this: stackoverflow.com/questions/17350492/… but no luck Commented Jun 4, 2015 at 21:15
  • Thats a wierd error but with the code you have you should ideally get no deferred tasks to be flushed or something like that because your time out will never run in the controller due to typo @ if(resolvedData.lenth > 0) { check the spelling of length Commented Jun 4, 2015 at 21:27
  • i apologize for spelling it wrong here. it is spelled correctly in my code. Commented Jun 4, 2015 at 21:28
  • Is it only blowing up in PhantomJS? It's also not clear to my why you're putting a timeout in your controller. what are you trying to accomplish? Commented Jun 4, 2015 at 23:40
  • Yep, it's only messing up in PhantomJS. I have a time out in my controller because I have pre-resolved data that I want to animate. The data will only animate (from ng-repeat) when I set a timeout. The strange thing is I setup another identical controller spec to get tested in visual studios but no issues there. Commented Jun 8, 2015 at 20:22

1 Answer 1

0

This happened to me when calling $timeout with a callback function. When omitting this function, it did work. For your controller this would mean:

$timeout(vm.data = resolvedData, 0);

I know this is a less favoutive solution, but it works in both the controller and the test.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.