jeudi 11 août 2016

AngularJS and Jasmine: isolateScope() is undefined

after two days of researching and testing, I need to ask for help. I am trying to test a directive using Jasmine, and I don't want to include the Karma engine.

In order to see the result of the test, I use the jasmine-html library, and the jasmine css.

I am able to test services easily, but when it comes to testing directive I am not able to access to the isolated scope. One thing to keep in mind is that I don't want to use controllers in my directive, but link functions. (according to Angular doc, controller should be used only when you want to expose an API to other directives.)

I found multiple answers on StackOverflow, but none of them worked.
The last thing I tried is based on this answered question StackOverflow. This is the test I am trying to replicate

describe('Wikis Directive Test Suite', function () {
    var $scope, scope, elem, directive, linkFn, html;

    beforeEach(module('app'));

    beforeEach(function () {
        html = '<wikis></wikis>';

        inject(function ($compile, $rootScope, $templateCache) {
           $templateCache.put('templates/wiki-list.html', '<div>wiki template</div>');

        $scope = $rootScope.$new();
        $scope.wikis = [];

        elem = angular.element(html);

        elem= $compile(elem)($scope);
        //scope = elem.isolateScope(); /*This is always undefined!*/
        scope = elem.scope(); /* this doesn't have addWiki, only the empty wikis array  */
        $rootScope.$digest();
       });
   });

    it('add Wiki should add a valid wiki URL to artist', function () {
        var url = 'http://www.foo.com';
        scope.newWikiURL = url;
        scope.addWiki();

        expect(scope.wikis.length).toBe(1);
        expect(scope.wikis[0]).toBe(url);
        expect(scope.newWikiURL).toBe('');
    });
});

The "only" difference, is that I need to use Jasmine 2.4.1 and Angular 1.0.7. Unfortunately it seems like with these libraries the test doesn't work. The thing is that isolateScope() is always undefined!

I've created a plunker to reproduce the problem.

http://ift.tt/2aK7oJy

Aucun commentaire:

Enregistrer un commentaire