In my scenerio I do the following:
1) Grab data from socket
2) Raise an event $scope.$broadcast('socket:newData', data)
3) Watch it in the directive:
$scope.processedData = [];
$scope.$on('socket:newData', function(e,data){
// gets data processed, and adds it to $scope.processedData
myService.dataProcess($scope.processedData, data);
});
In the app it works fine and I get the data. In my test I define $rootScope and $compile. The directive builds nicely and I do some tests for its isolated scope as well as HTML and everything seems to be in order (including templates and subdirectives loading).
The problem is: $scope.prcessedData remains empty.
Here is my test code:
beforeEach(module('app'));
beforeEach(_inject(function(_$rootScope_, _$compile_, _myService_){
$rootScope = _$rootScope_;
$compile - _$compile_;
myService = _myService_;
element = angular.element('<my-directive></my-directive>');
$compile(element)($rootScope);
}));
beforeEach(function(){
spyOn(myService, 'dataProcess');
var data = getMockData(); // returns some data sample that's in the format of the data recieved from the socket
$rootScope.$broadcast('socket:newData', data);
$rootScope.$apply();
});
it('Processed data should be filled', function(){
// *getExpected* returns the expected processed data
expect(element.isolateScope().processedData).toEqual(getExpected(getMockData()));
});
But element.isolateScope().processedData returns []...
I've tried the following to make sure the processing function runs its course:
it('Expect the function "dataProcess" to run', function(){
expect(myService.dataProcess).toHaveBeenCalledWith(element.isolateScope().processedData, getMockData());
});
Which passes and still element.isolateScope().processedData returns []...
How can I retrieve the values after the function has run its course?
Aucun commentaire:
Enregistrer un commentaire