Gabriel
Gabriel

Reputation: 611

Testing Directive with Jasmine and scope pass by reference

I'm trying to test one directive with jasmine. directive:

angular.module('app').directive('test',
   function(){
       return{
           restrict: 'A',
           scope:{data:'='},
           link:function($scope,element,attrs){
           .....
               $scope.data=[200,300];
            }
       }
    }
   ]);

jasmine:

describe('test', function(){
    beforeEach(module('test'));
    beforeEach(inject(function($rootScope,$compile){
       scope = $rootScope.$new();
       element = '<div test data="{{data}}"></div>';      
       scope.data = [100,200]
       element = $compile(element)(scope);
       scope.$digest();
    }));
    it('is a test',function(){
       expect(data).toBe([100,200]);
    });
 }

And as the scope is using "=", it passing the reference. However when run the tests, there're Syntax Error: Token 'data' is unexpected, expecting[:]at column 3 of the expression [{{data}}]. If I get rid of div in the test template with just <test data={{data}}></test>, it works fine. And when I replace "=" with "@" in scope it also works fine. Can anyone give me some suggestions on how to pass the reference to the scope? Thanks.

Upvotes: 3

Views: 2733

Answers (1)

Gabriel
Gabriel

Reputation: 611

After two days of trying, I finally could do it.

describe('test', function(){
beforeEach(module('test'));
beforeEach(inject(function($rootScope,$compile){
   $scope = $rootScope.$new();
   element = '<div test data="databind"></div>';      
   $scope.databind = [100,200]
   element = $compile(element)($scope);
   $scope.$digest();
}));
it('is a test',function(){
   tests.......
});

And it seems that {{}} didn't work here. And by using scope we can pass it by reference.

Upvotes: 6

Related Questions