Angularjs - test assignment of a variable in $ scope in an asynchronous function

6

I want to test the following scenario:

I have a scope:

var scope;

beforeEach(inject(function($rootScope) {
  scope = $rootScope.$new();
}));

You need to test whether the assignment of a variable in the $ scope within an asynchronous function will already have passed the angle digest.

obj.save().then(function(obj) {
   scope.obj = obj;
});

expect(scope.obj).not.toBe(null);

I do not know what to do, does anyone give a light?

    
asked by anonymous 22.02.2014 / 03:10

2 answers

1

I was able to resolve it as follows:

var $rootScope, $compile;

beforeEach(inject(function(_$rootScope_, _$compile_) {
  $compile = _$compile_;
  $rootScope = _$rootScope_;
}));

it("updates angular $scope", function() {
  $rootScope.obj = null;

  var element = $compile('<input ng-model="obj" type="text">')($rootScope);
  expect(element.val()).toBe('');

  runs(function() {
    p._save().then(function(data) {
      $rootScope.obj = "test";
    });
  });

  waitsFor(function() {
    return $rootScope.obj !== null;
  }, 'waiting for digest', 3000)

  runs(function() {
    expect(element.val()).toBe('test');
  });
});
    
22.02.2014 / 22:42
0

I do not know if I understand your problem correctly, but you can take a look at:

link

According to the documentation it is possible to perform an asynchronous function and check when it exits and then take an action.

    
22.02.2014 / 20:41