Testing an Angular directive with isolate scope data

140 Views Asked by At

I have an AngularJS directive that reads an input data from it's attributes, like this:

<my-directive input-data='items'></my-directive>

My directive:

return {

    scope: {
        inputData: '='
    }

}
...

Now, I usually define a nested array of objects in my scope and the directive reads that data from my scope, but now I'm trying to make jasmine, grunt and angular play nicely with my directive so I can test it, but I'm getting this error:

Error: [$compile:nonassign] Expression 'undefined' used with directive 'angularMultiSelect' is non-assignable!

This is how I'm trying to inject the data in my jasmine test:

inject(function($compile, $rootScope) {
    scope = $rootScope.$new();

    //scope.items = [];  //<--- this works, but it's useless
    scope.items = [{}, {}];  //<---- this triggers the error (I haven't deleted anything, that is an array with 2 empty objects
    elem = angular.element(html);
    $compile(elem)(scope);
    scope.$digest();
});

How am I supposed to pass my data, from jasmine, to my directive?

1

There are 1 best solutions below

0
On

My problem was that the directive was trying to bind another model data to a non-existing variable in the scope of jasmine.