Testing Directive with jasmine and skip area by link
I am 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 since the scope uses "=", it passes the link. However, when running tests, a syntax error occurs: "token" data is unexpected, expecting [:] in column 3 of the expression [{{data}}]. If I get rid of the div in the test template with only s <test data={{data}}></test>
, it works fine. And when I replace "=" with "@" it works fine too. Can anyone give me some advice on how to pass a scope reference? Thank.
+3
source to share
1 answer
After two days of trying, I was finally able to 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 {{}} doesn't work here. And using the scope, we can pass it by reference.
+6
source to share