Question

I've coded a directive that checks some permissions and delete an element from the DOM if permissions are KO.

I'd love to unit test it, but... hem, I'm banging my head agains walls to make this simple test work.

I use $rootScope.digest() to compile a piece of html. When calling this function, angular tries to load my app main page and I get the dreaded "no more request expected" error.

So here is the test :

describe('Unit testing permission-needed', function() {
    var $compile;
    var $rootScope;
    var $httpBackend;

    // Load the myApp module, which contains the directive
    beforeEach(module('app'));

    beforeEach(angular.mock.module('ngMockE2E'));

    beforeEach(inject(function(_$compile_, _$rootScope_, $injector) {
        $compile = _$compile_;
        $rootScope = _$rootScope_;
        $httpBackend = $injector.get('$httpBackend'); // not sur if I can inject it like others services ?
        $httpBackend.whenGET('app/login/login.tpl.html').passThrough(); // this doesn't seem to work

    }));



    it('should replace the element with the appropriate content', function() {



        // Compile a piece of HTML containing the directive
        var element = $compile("<div permission-needed><span>Some content goes here</span></div>")($rootScope);
        $rootScope.$digest(); // BAM => "no more request expected"

        // Do the test here
        // expect(....);
    });
});

Note that if I use .respond('some html here'); instead of .passThrough() it works.

Thank you.

Was it helpful?

Solution

Well, answering myself :

using a $new() rootScope, test is passing :

$rootScope = _$rootScope_.$new();

Hope this help someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top